I understand that while using the stateless session one must explicitly save an object association (child)
If I have the following objects:
public class Parent()
{
public int Id {get;set;}
public string Name {get;set;}
public IList<Child> Childs {get;set;}
}
public class Child()
{
public int Id {get;set;}
public string Name {get;set;}
}
I modify an instance of parent and add one child to it, I then save the parent and child using the following statements:
statelesssession.Update(parentInstance);
statelesssession.Insert(parentInstance.Childs.Last());
Doing this updates sucessfully the parent and creates the child record, however the field Parent_Id from the Child Table stays null, therefore there the association is not recorded.
How can I manually record the association using the stateless session?
I don’t see a
many-to-oneproperty onChildthat points back toParent. That’s what NHibernate would use for saving theParent_idcolumn. You need:… and the corresponding NHibernate mapping. Also, make sure that you set the value of
child.Parentwhen you add the child to the parent.Another thing, given the sequence of events you describe (“I create an instance of parent and add one child to it”) I would have expected to see an
Insertfor the parent instead of anUpdate.