Monday, July 07, 2008

NHibernate Entities Not Loading ID Values When Read From The DB

Problem: Your entities aren't loading up their ID properties when you do a find/read/whatever.  Other values for the entities load fine, but not the IDs.

Answer: Check the casing of your properties in your business objects and your mapping files.

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"

                   default-access="field.camelcase">

<id column="groupid" name="GroupID" >

  <generator class ="identity" />

</id>

Note the disconnect between the "default-access" attrib of the mapping element and the naming convention in id element's "name" attrib.  Ooops.  Either change the default-access or the casing of the name attrib:

<id column="groupid" name="GroupId" >

  <generator class ="identity" />

</id>

More on naming strategies in the NHib dox.

1 comment:

Brett Veenstra said...

You might find this helpful: http://codebetter.com/blogs/jeremy.miller/archive/2008/06/18/working-faster-and-fewer-mapping-errors-with-nhibernate.aspx

Subscribe (RSS)

The Leadership Journey