Let's say I have a one-to-many Parent-Child relationship and a Service method that looks like this:
Code:
saveParent(Parent parent, List<Child> children);
Let's also say that the Children are already persistent, but the Parent is not before calling this method. So, in a loop I do:
Code:
parent.addChild(child)
using the usual add-to-list-associate-with-parent stuff. Then I go and save the Parent, but bomb out with a InvalidStateException, since I am using Hibernate Validator and my Parent is invalid. Control returns to my Web Controller, and I forward on to some view where I am going to render the bad Parent along with it's Children. When I try to access a child of one of the original children element (where the "grandchild" is also already persistent), I am getting a LazyInitializationException, even though I am using Open Session In View.
To me, this seems strange because the Parent, even though it is transient, is just holding a List of Children. Each one is persistent, there is an OSIV, so why shouldn't I be able to get the grandchildren?
I begin to debug, and notice Hibernate has transformed my ArrayList of children on the parent into a PersistentBag with a null Session. So, this doesn't seem like the classic case of LazyInitializationException, but nonetheless I appear to be breaking this rule located here:
http://www.hibernate.org/hib_docs/refer ... ialization
it reads:
A LazyInitializationException will be thrown by Hibernate if an uninitialized collection or proxy is accessed outside of the scope of the Session, ie.
when the entity owning the collection or having the reference to the proxy is in the detached state.
I am NOT outside the scope of a Session since I have OSIV, but I do fit the rest of the mold outlined, which I have in bold.
Can someone please verify if it is the case that you can not lazily access persistent collections from a transient parent even with Open Session In View?