Announcement Announcement Module
Collapse
No announcement yet.
LazyInitializationException: Failed to lazily initialize Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • LazyInitializationException: Failed to lazily initialize

    Hifriends.
    I want to get a Set in my Dao class,But I get a LazyInitializationException by Hibernate. My code is:
    Code:
    public List getMsg(final String userName) {
    		HibernateTemplate hibernateTemplate = new HibernateTemplate(this.getSessionFactory());
    	    User user= (User) hibernateTemplate.execute(
    	        new HibernateCallback() {
    	          public Object doInHibernate(Session session) throws HibernateException {
    	            User u=(User)session.createQuery("from User u where u.userName=?").setString(0,userName).uniqueResult();
    	            // do some further stuff with the result list
    	            return u;
    	          }
    	        });
    	    List msgList=null;
    	    Set msgSet=user.getMessageSet();
    	    try {
    			Hibernate.initialize(msgSet);
    		} catch (HibernateException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	   if(null!=user.getMessageSet()){
    	   	msgList=new ArrayList(msgSet);
    	   }
    
    		return msgList;
    	}
    Excepion:
    Code:
    13:57:31,929 ERROR LazyInitializationException:25 - Failed to lazily initialize
    a collection - no session or session was closed
    net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a coll
    ection - no session or session was closed
            at net.sf.hibernate.collection.PersistentCollection.initialize(Persisten
    tCollection.java:209)
            at net.sf.hibernate.collection.PersistentCollection.read(PersistentColle
    ction.java:71)
            at net.sf.hibernate.collection.Set.size(Set.java:106)
            at java.util.ArrayList.<init>&#40;ArrayList.java&#58;132&#41;
            at lyo.hotmail.site.service.MessageDAOImpl.getMsg&#40;MessageDAOImpl.java&#58;59
    &#41;
            at lyo.hotmail.site.action.DoMsgAction.viewlist&#40;DoMsgAction.java&#58;57&#41;
            at sun.reflect.NativeMethodAccessorImpl.invoke0&#40;Native Method&#41;
            at sun.reflect.NativeMethodAccessorImpl.invoke&#40;NativeMethodAccessorImpl.
    java&#58;39&#41;
            at sun.reflect.DelegatingMethodAccessorImpl.invoke&#40;DelegatingMethodAcces
    sorImpl.java&#58;25&#41;
            at java.lang.reflect.Method.invoke&#40;Method.java&#58;324&#41;
            at org.apache.struts.actions.DispatchAction.dispatchMethod&#40;DispatchActio
    n.java&#58;280&#41;
            at org.apache.struts.actions.DispatchAction.execute&#40;DispatchAction.java&#58;
    216&#41;
            at org.apache.struts.action.RequestProcessor.processActionPerform&#40;Reques
    tProcessor.java&#58;484&#41;
            at org.apache.struts.action.RequestProcessor.process&#40;RequestProcessor.ja
    va&#58;274&#41;
            at org.apache.struts.action.ActionServlet.process&#40;ActionServlet.java&#58;148
    2&#41;
            at org.apache.struts.action.ActionServlet.doGet&#40;ActionServlet.java&#58;507&#41;
            at javax.servlet.http.HttpServlet.service&#40;HttpServlet.java&#58;689&#41;
            at javax.servlet.http.HttpServlet.service&#40;HttpServlet.java&#58;802&#41;
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter&#40;Appl
    icationFilterChain.java&#58;237&#41;
            at org.apache.catalina.core.ApplicationFilterChain.doFilter&#40;ApplicationF
    ilterChain.java&#58;157&#41;
            at lyo.hotmail.site.util.HiberFilter.doFilter&#40;HiberFilter.java&#58;50&#41;
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter&#40;Appl
    icationFilterChain.java&#58;186&#41;
            at org.apache.catalina.core.ApplicationFilterChain.doFilter&#40;ApplicationF
    ilterChain.java&#58;157&#41;
            at org.apache.catalina.core.StandardWrapperValve.invoke&#40;StandardWrapperV
    alve.java&#58;214&#41;
            at org.apache.catalina.core.StandardValveContext.invokeNext&#40;StandardValv
    eContext.java&#58;104&#41;
            at org.apache.catalina.core.StandardPipeline.invoke&#40;StandardPipeline.jav
    a&#58;520&#41;
            at org.apache.catalina.core.StandardContextValve.invokeInternal&#40;Standard
    ContextValve.java&#58;198&#41;
            at org.apache.catalina.core.StandardContextValve.invoke&#40;StandardContextV
    alve.java&#58;152&#41;
            at org.apache.catalina.core.StandardValveContext.invokeNext&#40;StandardValv
    eContext.java&#58;104&#41;
            at org.apache.catalina.authenticator.AuthenticatorBase.invoke&#40;Authentica
    torBase.java&#58;462&#41;
            at org.apache.catalina.core.StandardValveContext.invokeNext&#40;StandardValv
    eContext.java&#58;102&#41;
            at org.apache.catalina.core.StandardPipeline.invoke&#40;StandardPipeline.jav
    a&#58;520&#41;
            at org.apache.catalina.core.StandardHostValve.invoke&#40;StandardHostValve.j
    ava&#58;137&#41;
            at org.apache.catalina.core.StandardValveContext.invokeNext&#40;StandardValv
    eContext.java&#58;104&#41;
            at org.apache.catalina.valves.ErrorReportValve.invoke&#40;ErrorReportValve.j
    ava&#58;118&#41;
            at org.apache.catalina.core.StandardValveContext.invokeNext&#40;StandardValv
    eContext.java&#58;102&#41;
            at org.apache.catalina.core.StandardPipeline.invoke&#40;StandardPipeline.jav
    a&#58;520&#41;
            at org.apache.catalina.core.StandardEngineValve.invoke&#40;StandardEngineVal
    ve.java&#58;109&#41;
            at org.apache.catalina.core.StandardValveContext.invokeNext&#40;StandardValv
    eContext.java&#58;104&#41;
            at org.apache.catalina.core.StandardPipeline.invoke&#40;StandardPipeline.jav
    a&#58;520&#41;
            at org.apache.catalina.core.ContainerBase.invoke&#40;ContainerBase.java&#58;929&#41;
    
            at org.apache.coyote.tomcat5.CoyoteAdapter.service&#40;CoyoteAdapter.java&#58;16
    0&#41;
            at org.apache.coyote.http11.Http11Processor.process&#40;Http11Processor.java
    &#58;799&#41;
            at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.proce
    ssConnection&#40;Http11Protocol.java&#58;705&#41;
            at org.apache.tomcat.util.net.TcpWorkerThread.runIt&#40;PoolTcpEndpoint.java
    &#58;577&#41;
            at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run&#40;ThreadP
    ool.java&#58;684&#41;
            at java.lang.Thread.run&#40;Thread.java&#58;534&#41;
    This sentence give me an Exception:
    Code:
    msgList=new ArrayList&#40;msgSet&#41;; <------ LazyInitializationException
    But I already using
    Code:
    Hibernate.initialize&#40;msgSet&#41;;
    to initialize this Set. Why this collection was not been initialized? [/code]

  • #2
    I guess that no transactions are configured for this method.
    You need to move this code
    Code:
           try &#123; 
             Hibernate.initialize&#40;msgSet&#41;; 
          &#125; catch &#40;HibernateException e&#41; &#123; 
             // TODO Auto-generated catch block 
             e.printStackTrace&#40;&#41;; 
          &#125;
    inside the doInHibernate method as the Hibernate session is closed before this method returns.
    You can also refactor your class to extends HibernateDaoSupport and wrap your methods call inside Transactions.
    HTH

    Comment


    • #3


      You are right! I change my code to this:
      Code:
      public List getMsg&#40;final String userName&#41; &#123;
      		HibernateTemplate hibernateTemplate = new HibernateTemplate&#40;this.getSessionFactory&#40;&#41;&#41;;
      		final Set msgSet=null;
      			    List msgList= &#40;List&#41; hibernateTemplate.execute&#40;
      	        new HibernateCallback&#40;&#41; &#123;
      	          public Object doInHibernate&#40;Session session&#41; throws HibernateException &#123;
      	            User u=&#40;User&#41;session.createQuery&#40;"from User u where u.userName=?"&#41;.setString&#40;0,userName&#41;.uniqueResult&#40;&#41;;
      	            // do some further stuff with the result list
      	            List msgList=null;
      	            if&#40;null!=u&#41;&#123;
      	            	Set msgSet=u.getMessageSet&#40;&#41;;
      	            	if&#40;null!=msgSet&#41;&#123;
      					   msgList=new ArrayList&#40;msgSet&#41;;
      	            	&#125;
      				&#125;
      	            
         // Hibernate.initialize&#40;msgSet&#41;; <---It can work Even I uncommit it!
      	            return msgList;
      	          &#125;
      	        &#125;&#41;;
      		return msgList;
      	&#125;
      It can work property.
      I also find a more easy method:
      Code:
      List msgList=this.getHibernateTemplate&#40;&#41;.find&#40;"from Message msg where msg.user.userName='"+userName+"'"&#41;;
      Thank you! [/code]

      Comment

      Working...
      X