[ACCEPTED]-NHibernate - not-null property reference a null or transient value-nhibernate-mapping

Accepted answer
Score: 18

Another possibility is that you're saving 10 an entire object graph, and that graph is 9 circular, and items cannot be null. You 8 could be giving NHibernate no legal order 7 in which to do the inserts. (It should produce 6 a better error message, but it produces 5 this one).

Without seeing the rest of the 4 source, it's hard to be of more help. Try 3 removing entities from the mappings (and 2 not saving them) until you can figure out 1 what is causing the problem.

Score: 10

not sure if it helps but this did it for 1 me.

<many-to-one name="Company" column="CompanyId" cascade="all" not-null="true"/>

cascade="all" was what I missed out before

Score: 8

I've had this problem recently and it has 15 to do with the way NHibernate bi-directional 14 relationships are persisted. You have the 13 mapping correct and therefore NHibernate 12 will perform the Patient insert no problem. Then 11 NHibernate needs to take the key from Patients 10 and cascade that into Insurances. Since 9 the Patient does not yet exist, the keys 8 do not exist and it cannot perform the second 7 insert. The key is to set the relationship 6 via code prior to persistence, something 5 like this:

patient = new Patient();
patient.Insurances.Add( new Insurance{ Patient = patient } );
repository.Save( patient);

Now it was foreign to me that 4 you have to set the Patient property on 3 the collection item, but if you ignore persistence 2 all together you will be setting this in 1 code independent of your persistence strategy.

Score: 1

This worked for me. The important things 2 here are that we have References with Cascade.All() and we don't 1 have Inverse() on HasMany

public PatientMap()
    HasMany(x => x.Insurances)


public InsuranceMap()
    References(x => x.Patient, "uid_Patient")

Score: 0

It looks like the exception originates at 9 line 25 of your RepositoryBase.cs file, presumably 8 when a Save() is called on one of your transient 7 objects. Which one is being saved?

Also, and 6 it might be unrelated since I'm unfamiliar 5 with the Fluent syntax, should the child 4 object (the insurance in this case) have 3 .Cascade.All on it? In standard XML schema 2 syntax, only the parent mapping has cascade="all" on 1 the collection of child objects.

More Related questions