Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

failed to lazily initialize a collection of role

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I searched the forum for the same error and got many results but nothing seems to work in my problem. Can any one help me. I have many to one relation between TEAM and PLAYERS. I retrive a team based on uique id and then try to add players to that team. When i do that i get the following error

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.zeven.sport.football.bean.RegistrationBean.TeamPlayers, no session or session was closed
org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:343)
org.hibernate.collection.PersistentSet.add(PersistentSet.java:189)
com.zeven.sport.football.action.PlayerRegisterActionController.handleRequestInternal(PlayerRegisterActionController.java:62)
org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:771)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)


here is the code which i am using.


 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Too much code.
Essentially the code can see the SessionFactory at the point where the Team is loaded (TeamRegistrationDAOImpl), but it does not load the players at that stage.
Later when the code goes to access the Player collection (PlayerRegisterActionController) the SessionFactory is not accessible.
It is a bit hard to see where your Transaction/Session demarkation is due to the amount of code.
 
juniad Ahmed
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi David,
I am not retrieving and player from data base. I am just creating a new instance of player then saving it into data base with one to many relation.
 
Ranch Hand
Posts: 662
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

juniad Ahmed wrote:

......

team= teamRegisterDao.queryTeam(Long.parseLong(request.getParameter("teamId")));
.
.
.
.
.

team.getTeamPlayers().add(player);
.
.
.
.
.




You seem to load the team details and then when you do a team.getPlayers(), your players seem to have loaded lazily and when you do a get on them, they are out of context to go to the database fetch the details. Don't do a getPlayers(), instead, create a new Collection, add your player and save it.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic