I am effectively doing the following:
session.Save(newParent);
newChild = new Child(parent: newParent);
session.Save(newChild);
session.Load<Parent>(newParent.Id).Children.Count //0 - calling Get has the same result
However, if before the Load call, I call session.Refresh(newParent), the Children will load correctly. So it is clearly caching the parent before it has any children, then not updating the cache with the new child when it is added.
Parent Mapping:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="MyApp.Models.Entities.Parent,MyApp" table="[Parent]" lazy="true" batch-size="100">
<id name="ParentId" column="ParentId" type="int">
<generator class="native" />
</id>
<bag name="Children" inverse="true" lazy="true" cascade="delete" batch-size="100">
<key column="ParentId" />
<one-to-many class="MyApp.Models.Entities.Child,MyApp" />
</bag>
</class>
</hibernate-mapping>
Child Mapping:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="MyApp.Models.Entities.Child,MyApp" table="[Child]" lazy="true" batch-size="100">
<id name="ChildId" column="ChildId" type="int">
<generator class="native" />
</id>
<many-to-one name="Parent" column="ParentId" cascade="save-update" />
</class>
</hibernate-mapping>
My question is 1) Am I doing something wrong or is this the default behaviour? 2) If not, what is the cleanest way to solve this?
This issue actually only occurs in the test project and I can mod the underlying test architecture to do a force refresh behind the scenes in this case, but I would like to fully understand what is going on before implementation a solution.
Using NHibernate 3.2 and everything is wrapped in a ReadCommitted transaction.