-->
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: Question about LAZY Loading
PostPosted: Thu May 29, 2008 7:58 am 
Regular
Regular

Joined: Wed Apr 09, 2008 10:28 am
Posts: 52
Hi,

in my application i use only FetchType.EAGER. I want to change it into LAZY in result to gain more performance. My problem now is when i change it into LAZY i get an exception like

Code:
failed to lazily initialize a collection of role: .., no session or session was closed


Here my a part of my LogFile:

Code:
13:54:45,154 DEBUG JpaTransactionManager:534 - Closing JPA EntityManager [org.hibernate.ejb.EntityManagerImpl@c028cc] after transaction
13:54:45,154 DEBUG EntityManagerFactoryUtils:311 - Closing JPA EntityManager
13:54:45,154 ERROR LazyInitializationException:19 - failed to lazily initialize a collection of role: ...


So the problem is the EntityManager gets closed. This was no big deal before because with EAGER i suppose i was fetching the Objects i needed on initialization so within the Session. But now i only want to Fetch them when i really need them. How can i tell spring to reopen it (session / entitymanager) when i need it?
i hope somebody can help me thanks in advance ;).

edit: i've tryed to add em.getTransaction.begin() before my CRUD's but i get an exception because i'm using a shared EntityManager and it says i have to handle it by Spring.
greetz kuku


Top
 Profile  
 
 Post subject: re:laizy loading
PostPosted: Thu May 29, 2008 9:13 am 
Beginner
Beginner

Joined: Fri Jun 29, 2007 11:12 am
Posts: 25
Hello Kuku,
you're not using extended persistence context, if its a web app extended persistence context is a good practice anyway, try to find out how you can configure spring to use extended persistence context.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 29, 2008 9:46 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
This is one of the most common problems people have when they start using Lazy loading of their components.

Basically, your program is looking to access a lazily loaded object that has an association with an object that has already been loaded. However, the session that loaded the primary object has been closed, and the transaction has ended. So, now you are trying to access the lazily loaded object without any session context or any active transaction. Hibernate gets very upset with this, and throws the LazyLoadingException.

You need to keep your session open longer. If this is a JSP problem, you might even want to look at the Open Session in View pattern. It's documented here at hibernate.org:

http://www.hibernate.org/43.html

If you want more information on the problem with open and closes sessions, and accessing persistent and detached objects within them, take a look at my Hibernate and JPA tutorial on How Hibernate Works:

http://www.thebookonhibernate.com/HiberBookWeb/learn.jsp?tutorial=07howhibernateworks

Good luck!

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 30, 2008 5:08 am 
Regular
Regular

Joined: Wed Apr 09, 2008 10:28 am
Posts: 52
Hello,

thx both for your replys i will check out the stuff you mentioned. if i have another question i will comeback here.


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.