I am having a problem saving the Parent object which contains multiple children.
The Model Classes are as follows:
public class Driver
{
private IList<CitationEvent> _CitationEvents = new List<CitationEvent>(1);
}
public class CitationEvent
{
public virtual Driver Driver { get; set; }
}
XML Mapping in Driver.hbm.xml
<bag name="_CitationEvents" access="field" cascade="all-delete-orphan" inverse="true">
<key column="DRIVER_ID" />
<one-to-many class="CitationEvent" />
</bag>
XML Mapping in CitationEvent.hbm.xml
<many-to-one name="Driver" class="Driver" column="DRIVER_ID" cascade="none"/>
When I try to save driver
, NHibernate throws following exception "Nullable object must have a value"
The code which saves it:
ITransaction transaction = session.BeginTransaction(IsolationLevel.ReadCommitted);
try
{
session.Save(driver);
transaction.Commit();
}
catch (Exception ex)
{
transaction.Rollback();
}
One more observation about the problem:
When I try to just save Driver with empty list _CitationEvents
it does not give me this exception.