5 Replies Latest reply: Feb 7, 2013 12:01 AM by rick erlien RSS

    jboss-as-7.1.1.Final Lazy-Loading problem

    Martin Book Newbie

      Hello,

       

      i have switched my project from "jboss-as-web-7.0.2.Final" to "jboss-as-7.1.1.Final".

      Now Lazy-Loading seems not to function.

       

      My Bean:

      ...
      @OneToMany(mappedBy="mbMenueitem")
      @OrderBy(value="position asc")
      private Set<MbMenueitem> mbMenueitems; 
      

       

      My Controller

      @Model
      public class MenueItemProducer2 implements Serializable {
      
       @Inject
          private EntityManager em;
       ...
       List<MbMenueitem>tmpMenue =em.createQuery(criteria).getResultList();
       if(tmpMenue.size()>0){ 
          tmpItem=tmpMenue.get(0);
          Set<MbMenueitem> orderLines= tmpItem.getMbMenueitems();   
          System.out.println("Size: "+orderLines.size());  <-- stops here
       }
      
      

       

      ErrorMessage

       

      Caused by: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: org.jboss.as.quickstarts.kitchensink.model.MbMenueitem.mbMenueitems, no session or session was closed
       at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:393) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
       at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:385) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
       at org.hibernate.collection.internal.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:125) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
       at org.hibernate.collection.internal.PersistentSet.size(PersistentSet.java:156) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
       at org.jboss.as.quickstarts.kitchensink.data.MenueItemProducer2.findMenueItem(MenueItemProducer2.java:253) [classes:]
       at org.jboss.as.quickstarts.kitchensink.data.MenueItemProducer2.fillItems2(MenueItemProducer2.java:123) [classes:]
       at org.jboss.as.quickstarts.kitchensink.data.MenueItemProducer2.refreshMenue(MenueItemProducer2.java:86) [classes:]
      
      

       

       

      I have tried this, but it did not solve the problem:

       

      
      @PersistenceContext(type=PersistenceContextType.EXTENDED)
      private EntityManager em;
      
        • 1. Re: jboss-as-7.1.1.Final Lazy-Loading problem
          Scott Marlow Master

          Does the controller method start a JTA transaction (perhaps via default @TransactionAttribute(TransactionAttributeType.REQUIRED))?  There are two different code paths that could be hit, depending on your answer.  When you create a Query without a JTA transaction, we return a special proxy wrapper that does some (TypedQueryNonTxInvocationDetacher) magic.  If in a JTA transaction, we just return the underlying Hibernate query implementation that is returned from the Hibernate createQuery call.

           

          Can you also show us your persistence.xml contents?

           

          Lastly, can you enable TRACE logging for org.jboss.as.jpa & org.hibernate.  Restart the AS and exercise the above code.  Post the resulting as7/standalone/log/server.log here.  Instructions for enabling TRACE logging are here.

           

          Thanks for reporting this.

           

          Scott

          • 2. Re: jboss-as-7.1.1.Final Lazy-Loading problem
            Martin Book Newbie

            Adding  @Stateful to the Controller seems to solve the problem. Should it also work without the @Stateful Annotation?

             

             

             

             

             

            @Model

            @Stateful

            public

             

             

            class TestController implements Serializable {

            @PersistenceContext(type=PersistenceContextType.EXTENDED)

             

            private EntityManager em;

            ...

             

            • 3. Re: jboss-as-7.1.1.Final Lazy-Loading problem
              Scott Marlow Master

              The type=PersistenceContextType.EXTENDED, will only work in a stateful bean. 

               

              Leave it as stateful but try switching to a transaction scoped persistence context.  I expect that will also help but lets try it.  Change to:

               

              @Model

              @Stateful

              public class TestController implements Serializable {

              @PersistenceContext

              private EntityManager em;

              ...

              • 4. Re: jboss-as-7.1.1.Final Lazy-Loading problem
                Martin Book Newbie

                Hello,

                 

                seams like "PersistenceContextType.EXTENDED" is needed. Leaving it causes "org.hibernate.LazyInitializationException" again.

                • 5. Re: jboss-as-7.1.1.Final Lazy-Loading problem
                  rick erlien Newbie

                  It seems TransactionAttributeType.SUPPORTS or TransactionAttributeType.NOT_SUPPORTED will cause this LazyInitializationException issue to me.

                   

                  But TransactionAttributeType.REQUIRED won't have this issue.

                   

                  They were were working fine under jboss4.x.


                  Could you please tell the possible reason?