-->
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: Lazy collections question
PostPosted: Thu May 17, 2007 3:48 am 
Newbie

Joined: Thu Apr 26, 2007 4:15 am
Posts: 9
Hi,

(I know this has been discussed extensively, but I still have a question.)

Suppose I have an entity bean with a property (a collection) that is is annotated as being fetched lazily. I load the main entity bean with
EntityManager.find() outside any transaction (that's fine), but the moment I wish to access the lazy property I get the lazy initialization exception.

What is the reasoning behind such behavior? I've read about possible inconsistencies between the main entity and the properties, but why is it different from lazy loading the property in a transaction?

To workaround the problem I've implemented a utility bean that loads the entity and iterates over all its properties in a short-lived transaction and then returns the fully initialized entity to the caller. Is there a problem with such workaround? Or is it better to load the property's collection with EntityManager.createQuert().getResultList() ?

Thanks in advance!


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 17, 2007 8:44 am 
Newbie

Joined: Thu Apr 26, 2007 4:15 am
Posts: 9
For the sake of future references:
I've found a better workaround that uses the fetch hint as in

Code:
(Employee)entityManager.createQuery(
"select e from Employee e JOIN FETCH e.properties where .... ").getSingleResult()


That should prevent any LazyInitializationException s


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.