wood burning stoves*
The moose likes Object Relational Mapping and the fly likes failed to lazily initialize a collection of role Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "failed to lazily initialize a collection of role" Watch "failed to lazily initialize a collection of role" New topic
Author

failed to lazily initialize a collection of role

Manoj Raghuwanshi
Ranch Hand

Joined: Jun 20, 2004
Posts: 75
I am developing a sample struts-spring-hibernate program. I have a login screen where user enters his id. In the UserProcessAction app validates whether this user exists. This is implemented in spring-hibernate which return a User object (detached object). Then app will show an account information jsp which ask account information to the user. There is one � to- many mapping between User and Account class. After user enters information about the account , the CreateAccountAction add this account object to User object (User has Set of accounts). But I am not able to save this updated user object. It is throwing following exception �

failed to lazily initialize a collection of role: UserDetails.accounts - no session or session was closed
Please help me to solve this problem
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Well in order to access the database there has to be a session around. If there is no session, you don't have a Connection to the database. Since the data is not in the object, it was lazy loaded, later on when you try to access the attributes of the object it will want to go to the database, but there is no session and it can't. Bam Exception.

Are you accessing the data in a JSP page, do a search for the "Open Session In View" Design Pattern, or consider eager loading that data.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: failed to lazily initialize a collection of role
 
Similar Threads
LazyInitializationException illegal access to loading collection
Acess list elements on foreach JSTL tag
using c:forEach with spring/hibernate lazy loading
EJB Lazy Loading etc..
using c:forEach with spring/hibernate lazy loading