I have the following scenario: at runtime two “parent” entities are created. Each of them reference one “shared” child entity.
Here is some pseudo code:
public class Parent
{
public Parent(Child child)
{...}
public Child { get; }
}
Child child = new Child();
Parent mother = new Parent(child)
Parent father = new Parent(child)
I would like to be able to save the mother and father instances independently and potentially on a separate thread therefore from within different sessions and transactions.
How do I handle the saving of the Child entity with NH?
OK, 18 views and no comments… I guess I need to add some more clarification.
My mapping defines that Child should be automatically persisted when the Parent is persisted.
What will be happening with the child entity when
session.Save(father)
is called on one thread and
session.Save(mother)
on a different thread?
As it seems from the comment that child is only needed for the reference it is save to use
session.Load<Child>(knownChildId)in each thread. This will create 2 different Proxies for the same object.If the child is not yet saved and cascading is set then there could be a potential race condition and either
lock(child)beforesession.Save(father);