Nullable Property throws errors when Null in NHibernate

I have a property defined in my HBM file like this:

<property name="OwnerId" column="OwnerID" type="System.Int32" not-null="false" />

      

It is also defined as a nullable field in the database. If the record in the DB has the OwnerID column set to an integer, this object is loaded correctly by NHibernate. But if the entry is null, NHibernate bombs with seemingly random errors, including:

1) Column name "ModuleAnchorID" appears more than once in the list of result columns:

[SqlException (0x80131904): Column name 'ModuleAnchorID' appears more than once in the result column list.]
   System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +925466
   System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +800118
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +186
   System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +1932
   System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +149
   System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +1005
   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +132
   System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) +149
   System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +135
   NHibernate.Impl.NonBatchingBatcher.AddToBatch(IExpectation expectation) +35
   NHibernate.Persister.Entity.AbstractEntityPersister.Update(Object id, Object[] fields, Object[] oldFields, Boolean[] includeProperty, Int32 j, Object oldVersion, Object obj, SqlCommandInfo sql, ISessionImplementor session) +1055

      

2) the null-null property refers to a null or transient value:

[PropertyValueException: not-null property references a null or transient value:]
   NHibernate.Impl.SessionImpl.CheckNullability(Object[] values, IEntityPersister persister, Boolean isUpdate) +224
   NHibernate.Impl.SessionImpl.FlushEntity(Object obj, EntityEntry entry) +1019
   NHibernate.Impl.SessionImpl.FlushEntities() +182
   NHibernate.Impl.SessionImpl.FlushEverything() +90
   NHibernate.Impl.SessionImpl.AutoFlushIfRequired(ISet querySpaces) +64
   NHibernate.Impl.SessionImpl.Find(CriteriaImpl criteria, IList results) +217
   NHibernate.Impl.SessionImpl.Find(CriteriaImpl criteria) +42
   NHibernate.Impl.CriteriaImpl.List() +29

      

Is OwnerID a reserved field name that obfuscates NHibernate in some way?

0


source to share


2 answers


Can you change the type of the object to Nullable (int?). OwnerId is not a reserved keyword. If you think about how you can map DB null to Int32. Value objects do not support null semantics, so you really need to use a nullable type.



Regarding the ModuleAnchorId, I recommend that you enable sql mapping and then submit the sql to the nhibernate usergroup on google. NHibernate developers are paying attention to this group.

+2


source


You need to set the type of the property OwnerID

to int?

in the class definition. You can also get rid of the attribute type

in .hbm.xml

as NHibernate can infer the type from reflection.



+2


source







All Articles