-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Question regarding Persistent Collection Semantics
PostPosted: Sat Jan 31, 2009 12:35 am 
Newbie

Joined: Tue Jan 27, 2009 3:32 am
Posts: 4
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?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 31, 2009 7:42 am 
Senior
Senior

Joined: Thu Jan 08, 2009 3:48 pm
Posts: 168
transient would mean it was never loaded in the session, detached means it was loaded but then the session was closed. If transient it should be obvious why you can't access any persistent collection, you need to load the object first via get/load or save it in the same session.
Could it be possible that your InvalidStateException happens before the save?

If you redirect to a new page this _might_ be treated as a different Session.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.