-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Failed to lazily initialize a collection - no Session !!!!
PostPosted: Thu Jan 01, 2004 2:04 am 
Regular
Regular

Joined: Tue Nov 04, 2003 12:37 pm
Posts: 57
Hello,

I don't know why it will cause the following exceptions when calling the Player.getScorePosition().

Code:
   class Player

   public void setTeams(Set teams) {
      this.teams = teams;
   }
   
   
   /**
    * @hibernate.set
    *    lazy="true"
    *    table="team_player"
    *    order-by="teamId"
    *
    * @hibernate.collection-key
    *    column="playerId"
    *
    * @hibernate.collection-many-to-many
    *    class="com.echows.football.model.Team"
    *    column="teamId"
    *
    *
    */
   public Set getTeams() {
      return teams;
   }

   public double getTotalScores() {
      double sum = 0.0;           
     
      if (teams == null || teams.size() == 0) {
         return 0.0;
      }
     
      Iterator its = teams.iterator();
     
      while(its.hasNext()) {
         Team t = (Team)its.next();
         
         sum += t.getScore();
      }
     
      return ((sum / teams.size()) + (teams.size() * 0.5));
   }

   public int getScorePosition() {
      String qry = "select player from Player as player join player.teams as teams where player.playerStatus=? order by (sum(teams.score) / count(teams) + count(teams)*0.5)";
     
      int pos = -1;
     
      HibernateService hs = HibernateService.getInstance();     
     
      try {         
         Session ses = hs.beginTransaction();

         List result = ses.find(qry, new Boolean(true), Hibernate.BOOLEAN);                  
         
         Iterator its = result.iterator();
         
         int position = 1;
         double oldScore = 0;
         int count = 1;
         while(its.hasNext()) {
            Player p = (Player)its.next();
            
            double tScore = p.getTotalScores();
            
            if (tScore > oldScore) {
               position = count;
            }
            
            count++;
            
            oldScore = tScore;
            
            if (p.getPlayerId() == playerId) {
               break;
            }
         }

         pos = position;
      } catch(Exception e) {
         pos = -1;
      } finally {
         try {           
            hs.endTransaction(false);
         } catch(HibernateException he) {
            ;
         }
      }



Code:
2004/1/1 下午 01:59:54 net.sf.hibernate.LazyInitializationException <init>
ERROR: Failed to lazily initialize a collection - no Session
net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a coll
ection - no Session
        at net.sf.hibernate.collection.PersistentCollection.initialize(Persisten
tCollection.java:213)
        at net.sf.hibernate.collection.PersistentCollection.read(PersistentColle
ction.java:69)
        at net.sf.hibernate.collection.Set.size(Set.java:107)
        at com.echows.football.model.Player.getTotalScores(Player.java:148)
        at com.echows.football.web.tag.StatisticsTag.doStartTag(StatisticsTag.ja
va:72)
        at org.apache.jsp.playerIntroduction_jsp._jspService(playerIntroduction_
jsp.java:117)
        at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:137)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
        at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper
.java:210)
        at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:2
95)
        at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
        at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDisp
atcher.java:684)
        at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationD
ispatcher.java:575)
        at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDis
patcher.java:498)
        at org.apache.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary
.java:822)
        at org.apache.jasper.runtime.PageContextImpl.include(PageContextImpl.jav
a:398)
        at org.apache.struts.tiles.TilesUtilImpl.doInclude(TilesUtilImpl.java:13
7)
        at org.apache.struts.tiles.TilesUtil.doInclude(TilesUtil.java:177)
        at org.apache.struts.taglib.tiles.InsertTag.doInclude(InsertTag.java:756
)
        at org.apache.struts.taglib.tiles.InsertTag$InsertHandler.doEndTag(Inser
tTag.java:881)
        at org.apache.struts.taglib.tiles.InsertTag.doEndTag(InsertTag.java:473)

        at org.apache.jsp.vboxLayout_jsp._jspx_meth_tiles_insert_0(vboxLayout_js
p.java:139)
        at org.apache.jsp.vboxLayout_jsp._jspService(vboxLayout_jsp.java:94)
        at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:137)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
        at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper
.java:210)
        at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:2
95)
        at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
        at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDisp
atcher.java:684)
        at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationD
ispatcher.java:575)
        at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDis
patcher.java:498)
        at org.apache.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary
.java:822)
        at org.apache.jasper.runtime.PageContextImpl.include(PageContextImpl.jav
a:398)
        at org.apache.struts.tiles.TilesUtilImpl.doInclude(TilesUtilImpl.java:13
7)
        at org.apache.struts.tiles.TilesUtil.doInclude(TilesUtil.java:177)
        at org.apache.struts.taglib.tiles.InsertTag.doInclude(InsertTag.java:756
)
        at org.apache.struts.taglib.tiles.InsertTag$InsertHandler.doEndTag(Inser
tTag.java:881)
        at org.apache.struts.taglib.tiles.InsertTag.doEndTag(InsertTag.java:473)

        at org.apache.jsp.template_jsp._jspx_meth_tiles_insert_0(template_jsp.ja
va:83)
        at org.apache.jsp.template_jsp._jspService(template_jsp.java:58)
        at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:137)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
        at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper
.java:210)
        at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:2
95)
        at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
        at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDisp
atcher.java:684)
        at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationD
ispatcher.java:575)
        at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDis
patcher.java:498)
        at org.apache.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary
.java:822)
        at org.apache.jasper.runtime.PageContextImpl.include(PageContextImpl.jav
a:398)
        at org.apache.struts.tiles.TilesUtilImpl.doInclude(TilesUtilImpl.java:13
7)
        at org.apache.struts.tiles.TilesUtil.doInclude(TilesUtil.java:177)
        at org.apache.struts.taglib.tiles.InsertTag.doInclude(InsertTag.java:756
)
        at org.apache.struts.taglib.tiles.InsertTag$InsertHandler.doEndTag(Inser
tTag.java:881)
        at org.apache.struts.taglib.tiles.InsertTag.doEndTag(InsertTag.java:473)

        at org.apache.jsp.tabsLayout_jsp._jspService(tabsLayout_jsp.java:248)
        at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:137)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
        at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper
.java:210)
        at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:2
95)
        at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
        at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDisp
atcher.java:684)
        at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationD
ispatcher.java:575)
        at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDis
patcher.java:498)
        at org.apache.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary
.java:822)
        at org.apache.jasper.runtime.PageContextImpl.include(PageContextImpl.jav
a:398)
        at org.apache.struts.tiles.TilesUtilImpl.doInclude(TilesUtilImpl.java:13
7)
        at org.apache.struts.tiles.TilesUtil.doInclude(TilesUtil.java:177)
        at org.apache.struts.taglib.tiles.InsertTag.doInclude(InsertTag.java:756
)
        at org.apache.struts.taglib.tiles.InsertTag$InsertHandler.doEndTag(Inser
tTag.java:881)
        at org.apache.struts.taglib.tiles.InsertTag.doEndTag(InsertTag.java:473)

        at org.apache.jsp.index_jsp._jspx_meth_tiles_insert_0(index_jsp.java:178
)
        at org.apache.jsp.index_jsp._jspService(index_jsp.java:130)
        at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:137)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
        at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper
.java:210)
        at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:2
95)
        at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:247)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:193)
        at com.echows.football.web.filter.SetCharacterEncodingFilter.doFilter(Se
tCharacterEncodingFilter.java:97)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:213)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:193)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
alve.java:256)
        at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContex
t.invokeNext(StandardPipeline.java:643)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:480)
        at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)

        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
alve.java:191)
        at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContex
t.invokeNext(StandardPipeline.java:643)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:480)
        at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)

        at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:
2417)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
ava:180)
        at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContex
t.invokeNext(StandardPipeline.java:643)
        at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatche
rValve.java:171)
        at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContex
t.invokeNext(StandardPipeline.java:641)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j
ava:172)
        at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContex
t.invokeNext(StandardPipeline.java:641)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:480)
        at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)

        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal
ve.java:174)
        at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContex
t.invokeNext(StandardPipeline.java:643)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:480)
        at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)

        at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:19
3)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java
:781)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.proce
ssConnection(Http11Protocol.java:549)
        at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java
:589)
        at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadP
ool.java:666)
        at java.lang.Thread.run(Thread.java:534)



Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 01, 2004 4:25 am 
Regular
Regular

Joined: Thu Nov 20, 2003 10:04 pm
Posts: 64
Location: Melbourne, Australia
This generally happens because the session you originally retrieved the object with is now closed. Either make your collections non-lazy or keep your session open for the duration.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 01, 2004 12:07 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You can also reassociate your object with the current session using session.lock(object, LockMode.NONE).


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.