I’m attempting to use NHibernate to serialize a moderately complex object graph*
Actual mapping was done via FNH, but I’ve dumped the HBM files and confirmed that the generated XML conforms to NHibernate conventions.
Here’s a snippet of the HBM, just for grins:
<class xmlns="urn:nhibernate-mapping-2.2" schema="obsv" optimistic-lock="version" name="Spc.Ofp.Tubs.DAL.Entities.PurseSeineActivity, TubsDAL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="s_daylog">
<id name="Id" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="s_daylog_id" not-null="true" />
<generator class="identity" />
</id>
This mapping results in the following SQL (via SQL debug, snipped for readability):
INSERT INTO obsv.s_daylog (/* columns 0 thru 20 snipped */s_daylog_id /* <-- PK from mapping! */)
VALUES (/* parameters snipped */@p21);
select SCOPE_IDENTITY();@p21 = NULL [Type: Int32 (0)]
I believe that the presence of the “select SCOPE_IDENTITY();” text confirms that
NHibernate partially understands what should happen. I just don’t understand why it’s writing the PK column into the insert query.
I’ve been using the mappings for reading the graph just fine, so I’m fairly certain this isn’t a basic mapping issue.
FWIW, Cascade is set to None (for other reasons, I need to work with these entities without
ramifications up and down the object graph).
*By moderately complex, I mean I have a object which has between 6 and 10 properties which are lists of child entities. A good number of those child entities also have child entities. In the most complex case, there are 5 generations of entities under the root entity.
I think it is because your mapping seems incorrect.
According to the NHibernate Reference, the id tag has a “column” attribute to set the column name, not a child element.