-->
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.  [ 4 posts ] 
Author Message
 Post subject: Lazy Loading behavior
PostPosted: Thu Sep 04, 2008 9:24 am 
Newbie

Joined: Thu Dec 13, 2007 10:49 am
Posts: 5
Is it expected that I must reattach (lock) in this case an entity if the previous session was closed from the first request and I am on a subsequent request that uses the One Session per Request pattern?

The problem Im having is I have an entity (A) that contains a set of entities (B) during a subsequent postback I want to actually load the content (lazily) of the set which itself points to an entity (C) which isn't yet loaded.

It appears that if I do not reattach the entity (C) I get a LazyInitializationException that there is No Session. It appears that even though I have a session the Javassist handler proxy doesn't automatically look it up.

What do you do in this situation? I could reload the A entity and rewalk the tree but the list of Entities B is always a different order which causes confusion for JSF.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 04, 2008 3:01 pm 
Beginner
Beginner

Joined: Wed Apr 20, 2005 9:30 am
Posts: 39
I think you can use the Hibernate.initialize(yourObject.lazyObjectAccessor()) method to have Hibernate flesh out the proxy objects.

You may need to use a session.merge() method first, but I'm not 100% on that part.

I would certainly give it a try.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 05, 2008 11:41 am 
Newbie

Joined: Thu Dec 13, 2007 10:49 am
Posts: 5
Hibernate.initialize(object) works if I put it in the exact same location as Session.lock(object,NOLOCK) It doesn't seem to work if I try this on the collection SET or if I do it to its parent.

I was thinking it might be a cascade issue so I set it to cascade="all" but that didn't change anything.

Maybe I am expecting too much from Hibernate. I think it would be nice to be able to reattach the parent object to the session that it should then use this session on all of its children.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 05, 2008 5:29 pm 
Newbie

Joined: Thu Dec 13, 2007 10:49 am
Posts: 5
OK I apparently missed something whiule reading JPWH. It appears that my issue is associated with the fact that cacscade was not set properly. In this case I set it to all (other options may work)

Setting the parent to Hibernate.initialize(object) made no discernable change to the children's children. However, Hibernate.initialize(object) does work if you do this for the lazy object itself. This requires the methods in the POJO to know about Hibernate, which for me is not desireable.

Setting the parent to getSessionFactory().getCurrentSession().lock(object,LockMode.NONE); Did affect the children such that while they were in a lazy detached state originally, they were reattached to the session and no "No Session" exception was thrown. The parent object in this case is not a proxy as far as I can tell from the variable inspection.

Lesson learned watch the cascade option on many-to-one


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.