2 Replies Latest reply on Aug 11, 2009 6:36 PM by jmacneedshelp

    org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role

    jmacneedshelp

      I have a bean that pulls orders and presents them in an dataTable. In the dataTable I use a command link to set the selected order back into the bean so that I can create a popup window to show the order for printing. From within the popup window I access the getter to pull the Order but when I try to access any of the Collections in the Orders entity, I get a LIE. What's weird is that I can access these collections on the setter, but they're f'ed up in the getter.


      I'm getting and LIE and I don't know why this shit aint easy pie! Please help before I cry....OK, enough of that crap, down to business.


      VendorOrder bean


      @Stateful
      @Name("VendorOrder")
      @Scope(ScopeType.SESSION)
      public class VendorOrderAction implements VendorOrder {
         
          @In private EntityManager entityManager;
              
          private Orders vendorOrder = new Orders();
          
          public Orders getVendorOrder() {
              return vendorOrder;
          }
          
          public void setVendorOrder(Orders vendorOrder) {
              this.vendorOrder = vendorOrder;
          }
          
          
          public List<Orders> getOrderList() { 
              return entityManager.createQuery(
                      "select o from Orders o join fetch o.orderitems as item where item.vendor.vendorid=:vendor and o.status In('Vendors Assigned')"
              ).setParameter("vendor", vendor.getVendorid()).getResultList();
          }
      



      OrderManager(opens OrderView window using commandLink)


      <a:commandLink id="showItem" action="#{VendorOrder.setVendorOrder(_orders)}"
      oncomplete="window.open('OrderView.seam','sss:', 'width=1000,height=750, left=20, top=20');"
      >
      Click Here!
      </a:commandLink>
      



      OrderView popup attemtpting to access one of the collections in the Orders entity pulled from the VendorOrder bean.


      <h:outputText value="#{VendorOrder.vendorOrder.orderdeliveries.get(0).firstName}" /> <h:outputText value="#{VendorOrder.vendorOrder.orderdeliveries.get(0).lastName}" /><br/>
      <h:outputText value="#{VendorOrder.vendorOrder.orderdeliveries.get(0).organization}" /><br/>
      <h:outputText value="#{VendorOrder.vendorOrder.orderdeliveries.get(0).address1}" /><br/>
      <h:outputText value="#{VendorOrder.vendorOrder.orderdeliveries.get(0).address2}" /><br/>
      <h:outputText value="#{VendorOrder.vendorOrder.orderdeliveries.get(0).city}" />, <h:outputText value="#{VendorOrder.vendorOrder.orderdeliveries.get(0).state}" /><h:outputText value="#{VendorOrder.vendorOrder.orderdeliveries.get(0).postalCode}" />  <br/>
      <h:outputText value="#{VendorOrder.vendorOrder.orderdeliveries.get(0).country}" />
      



      Order entity orderdeliveries get method


      @OneToMany(fetch = FetchType.LAZY, mappedBy = "orders")
      public List<Orderdelivery> getOrderdeliveries() {
          return this.orderdeliveries;
      }
      



      Stacktrace


      javax.faces.FacesException: javax.el.ELException: /vendor/OrderView.xhtml @44,93 value="#{VendorOrder.vendorOrder.orderdeliveries.get(0).firstName}": org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.xeosuite.xeoadmin.entity.Orders.orderdeliveries, no session or session was closed
              at javax.faces.component.UIOutput.getValue(UIOutput.java:187)
              at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getValue(HtmlBasicInputRenderer.java:201)
              at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getCurrentValue(HtmlBasicRenderer.java:284)
              at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeEnd(HtmlBasicRenderer.java:154)
              at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:861)
              at org.ajax4jsf.renderkit.RendererBase.renderChild(RendererBase.java:286)
              at org.ajax4jsf.renderkit.RendererBase.renderChildren(RendererBase.java:262)
              at org.richfaces.renderkit.html.PanelRenderer.doEncodeChildren(PanelRenderer.java:199)
              at org.richfaces.renderkit.html.PanelRenderer.doEncodeChildren(PanelRenderer.java:194)
              at org.ajax4jsf.renderkit.RendererBase.encodeChildren(RendererBase.java:121)
              at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:837)
              at javax.faces.component.UIComponent.encodeAll(UIComponent.java:936)
              at javax.faces.component.UIComponent.encodeAll(UIComponent.java:942)
              at com.sun.facelets.FaceletViewHandler.renderView(FaceletViewHandler.java:592)
              at org.ajax4jsf.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:108)
              at org.ajax4jsf.application.AjaxViewHandler.renderView(AjaxViewHandler.java:216)
              at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:109)
      



      ... And further up the stack


      org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.xeosuite.xeoadmin.entity.Orders.orderdeliveries, no session or session was closed
              at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
              at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
              at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:343)
              at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
              at org.hibernate.collection.PersistentBag.get(PersistentBag.java:422)
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
              at java.lang.reflect.Method.invoke(Unknown Source)
              at org.jboss.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:329)
              at org.jboss.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:274)
              at org.jboss.el.parser.AstMethodSuffix.getValue(AstMethodSuffix.java:59)
              at org.jboss.el.parser.AstValue.getValue(AstValue.java:67)
              at org.jboss.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186)
              at com.sun.facelets.el.TagValueExpression.getValue(TagValueExpression.java:71)
              at javax.faces.component.UIOutput.getValue(UIOutput.java:184)
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      


      Thank you for your help.

        • 1. Re: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role
          asookazian

          My guess is that in your JPQL query:


          entityManager.createQuery(
                          "select o from Orders o join fetch o.orderitems as item where item.vendor.vendorid=:vendor and o.status In('Vendors Assigned')"
                  ).setParameter("vendor", vendor.getVendorid()).getResultList();
          



          the item.getVendor().getVendorId() is possibly causing the LIE to be thrown.


          SMPC is conversation-scoped.


          The way to solve this problem is to use SMPC with a conversation-scoped component.  So try converting this component from session-scoped to conversation-scoped and using @Begin or similar to start a LRC.


          The bottom line is this: LIEs happen when the PersistenctContext is closed and you do CRUD operations.  When you use LRCs with SMPCs, the PersistenctContext stays open until the conversation ends.  Read the Seam ref doc or SiA book for more info.

          • 2. Re: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role
            jmacneedshelp

            Hi Arbi,
            The EntityQuery is called to load the dataTable, that works fine. In fact even if I reload the dataTable everything works correctly. It's the popup page that's giving me the trouble. I set the selected order back into the bean in the setVendorOrder method and then retrieve it from the popup window in the getVendorOrder method. In setVendorOrder I can access the collections of the Orders entity but in the getVendorOrder method I cannot; it throws a LIE every time. This should work fine and I don't understand why it won't load these collections.


            Thank you for your help.