title: NHibernate - not-null property reference a null or transient value
body: <p>I'm getting this exception (Full exception at the bottom):</p>
  
  <pre><code>NHibernate.PropertyValueException was unhandled by user code
   Message="not-null property references a null or transient
  valueClearwave.Models.Encounters.Insurance.Patient"
   Source="NHibernate"
   EntityName="Clearwave.Models.Encounters.Insurance"
   PropertyName="Patient"
  </code></pre>
  
  <p>I've done a lot of Googling and it seems the most common cause for
  that error is when an association is bi-directional but only one half
  has been set. As in: Insurance.Patient = Patient is called but
  Patient.Insurances.Add(Insurance) is not. I do, in fact, have a
  scenario like that but I've checked the object just before calling
  Save with it and both Insurance.Patient and Patient.Insurances[0] are
  the right objects.</p>
  
  <p>The other possibility that this exception seems to reference is a
  transient value. In my case <em>every</em> object is transient so I'm
  suspecting the root of my problem is here. However, everything needs
  to be transient right now because nothing has been saved yet. I would
  expect NHibernate to persist things rather than complain that they are
  not persisted.</p>
  
  <p>Here are some snippets from my mappings (fluent):</p>
  
  <pre><code>       public PatientMap()
         {
             WithTable("tPatient");
  
             Id(x =&gt; x.Id, "uid_Patient").GeneratedBy.GuidComb
  ().Access.AsReadOnlyPropertyThroughCamelCaseField();
  
             HasMany(x =&gt; x.Insurances).WithKeyColumn("uid_Patient")
                 .Cascade.All()
                 .Inverse();
  
            ...
         }
  
        public InsuranceMap()
         {
             WithTable("tPatientInsuranceInfo");
  
             Id(x =&gt; x.Id,
  "uid_PatientInsuranceInfo").GeneratedBy.GuidComb
  ().Access.AsReadOnlyPropertyThroughCamelCaseField();
  
             References(x =&gt; x.Patient, "uid_Patient").Not.Nullable
  ().Cascade.All();
  
             ...
          }
  </code></pre>
  
  <p>So, what could be the issue?</p>
  
  <hr>
  
  <pre><code>NHibernate.PropertyValueException was unhandled by user code
   Message="not-null property references a null or transient
  valueClearwave.Models.Encounters.Insurance.Patient"
   Source="NHibernate"
   EntityName="Clearwave.Models.Encounters.Insurance"
   PropertyName="Patient"
   StackTrace:
        at NHibernate.Engine.Nullability.CheckNullability(Object[]
  values, IEntityPersister persister, Boolean isUpdate)
        at
  NHibernate.Event.Default.AbstractSaveEventListener.PerformSaveOrReplicate
  (Object entity, EntityKey key, IEntityPersister persister, Boolean
  useIdentityColumn, Object anything, IEventSource source, Boolean
  requiresImmediateIdAccess)
        at
  NHibernate.Event.Default.AbstractSaveEventListener.PerformSave(Object
  entity, Object id, IEntityPersister persister, Boolean
  useIdentityColumn, Object anything, IEventSource source, Boolean
  requiresImmediateIdAccess)
        at
  NHibernate.Event.Default.AbstractSaveEventListener.SaveWithGeneratedId
  (Object entity, String entityName, Object anything, IEventSource
  source, Boolean requiresImmediateIdAccess)
        at
  NHibernate.Event.Default.DefaultMergeEventListener.EntityIsTransient
  (MergeEvent event, IDictionary copyCache)
        at NHibernate.Event.Default.DefaultMergeEventListener.OnMerge
  (MergeEvent event, IDictionary copyCache)
        at NHibernate.Impl.SessionImpl.FireSaveOrUpdateCopy(IDictionary
  copiedAlready, MergeEvent event)
        at NHibernate.Impl.SessionImpl.SaveOrUpdateCopy(String
  entityName, Object obj, IDictionary copiedAlready)
        at
  NHibernate.Engine.CascadingAction.SaveUpdateCopyCascadingAction.Cascade
  (IEventSource session, Object child, String entityName, Object
  anything, Boolean isCascadeDeleteEnabled)
        at NHibernate.Engine.Cascade.CascadeToOne(Object child, IType
  type, CascadeStyle style, Object anything, Boolean
  isCascadeDeleteEnabled)
        at NHibernate.Engine.Cascade.CascadeAssociation(Object child,
  IType type, CascadeStyle style, Object anything, Boolean
  isCascadeDeleteEnabled)
        at NHibernate.Engine.Cascade.CascadeProperty(Object child,
  IType type, CascadeStyle style, Object anything, Boolean
  isCascadeDeleteEnabled)
        at NHibernate.Engine.Cascade.CascadeOn(IEntityPersister
  persister, Object parent, Object anything)
        at
  NHibernate.Event.Default.AbstractSaveEventListener.CascadeBeforeSave
  (IEventSource source, IEntityPersister persister, Object entity,
  Object anything)
        at
  NHibernate.Event.Default.DefaultMergeEventListener.EntityIsTransient
  (MergeEvent event, IDictionary copyCache)
        at NHibernate.Event.Default.DefaultMergeEventListener.OnMerge
  (MergeEvent event, IDictionary copyCache)
        at NHibernate.Impl.SessionImpl.FireSaveOrUpdateCopy(IDictionary
  copiedAlready, MergeEvent event)
        at NHibernate.Impl.SessionImpl.SaveOrUpdateCopy(String
  entityName, Object obj, IDictionary copiedAlready)
        at
  NHibernate.Engine.CascadingAction.SaveUpdateCopyCascadingAction.Cascade
  (IEventSource session, Object child, String entityName, Object
  anything, Boolean isCascadeDeleteEnabled)
        at NHibernate.Engine.Cascade.CascadeToOne(Object child, IType
  type, CascadeStyle style, Object anything, Boolean
  isCascadeDeleteEnabled)
        at NHibernate.Engine.Cascade.CascadeAssociation(Object child,
  IType type, CascadeStyle style, Object anything, Boolean
  isCascadeDeleteEnabled)
        at NHibernate.Engine.Cascade.CascadeProperty(Object child,
  IType type, CascadeStyle style, Object anything, Boolean
  isCascadeDeleteEnabled)
        at NHibernate.Engine.Cascade.CascadeOn(IEntityPersister
  persister, Object parent, Object anything)
        at
  NHibernate.Event.Default.AbstractSaveEventListener.CascadeBeforeSave
  (IEventSource source, IEntityPersister persister, Object entity,
  Object anything)
        at
  NHibernate.Event.Default.DefaultMergeEventListener.EntityIsTransient
  (MergeEvent event, IDictionary copyCache)
        at NHibernate.Event.Default.DefaultMergeEventListener.OnMerge
  (MergeEvent event, IDictionary copyCache)
        at NHibernate.Event.Default.DefaultMergeEventListener.OnMerge
  (MergeEvent event)
        at NHibernate.Impl.SessionImpl.FireSaveOrUpdateCopy(MergeEvent
  event)
        at NHibernate.Impl.SessionImpl.SaveOrUpdateCopy(Object obj)
        at Clearwave.Models.Data.Util.RepositoryBase`2.Save(EntityType&amp;
  entity) in C:\Projects\ClearWave\Src\Common\Domain Models
  \Clearwave.Models.Data-NHibernate\Util\RepositoryBase.cs:line 25
        at IntegrationWebServices.FromMirth.SubmitMessage(Message
  theMessage, Guid providerOrganizationId)
        at SyncInvokeSubmitMessage(Object , Object[] , Object[] )
        at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke
  (Object instance, Object[] inputs, Object[]&amp; outputs)
        at
  System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin
  (MessageRpc&amp; rpc)
   InnerException:
  </code></pre>