File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 Customer Requirements for Developers this week in the Jobs Discussion 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

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

Joined: Feb 05, 2001
Posts: 17271

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.


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
It is sorta covered in the JavaRanch Style Guide.
subject: failed to lazily initialize a collection of role
It's not a secret anymore!