Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

failed to lazily initialize a collection of role

 
Manoj Raghuwanshi
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic