I ran my code through the debugger and found the following.
Consider the following domain model :
Entity A has a one-one relationship with B. B has a 1-to-many relationship with C. (accessors, identity field, hashCode/equals deliberately left out here. for illustration purposes)
Code:
public class A
{
private B b;
}
public class B
{
private Set cees;
}
public class C
{
}
I load an instance of A (aInstance) in session1. session1 is closed and aInstance is not detached. I open a new session, session2 and call
Code:
session2.lock(aInstance, LockMode.NONE);
B bInstance = aInstance.getB();
Set cees = bInstance.getCees();
The above call to bInstance.getCees() results in LazyInitializationException despite the fact that we made the root object persistent by calling session2.lock(aInstance, LockMode.NONE).
Now if I explicitly lock bInstance, I am able to successfully read the Set of C objects.
ie
Code:
session2.lock(aInstance, LockMode.NONE);
B bInstance = aInstance.getB();
session2.lock(bInstance, LockMode.NONE);
Set cees = bInstance.getCees();
runs successfully without any errors. So calling session.lock(..) on a detached object does not completely change its state from detached to persistent. And this is what I asked him my original mail -
Quote:
So it seems that the reassociation of the user object with the new session is not nested
Calling session.lock(..) on all nested properties/collections of a detached object is not a clean solution.
So is this behavior of session.lock(..) not completely making a detached objected persistent an expected behavior? If so, whats the reason behind not making the entire relationship persistent?
Thanks,
Sanjiv