Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

failed to lazily initialize a collection of role

 
juniad Ahmed
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • 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.


 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Arun Kumarr
Ranch Hand
Posts: 661
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic