6 Replies Latest reply on May 24, 2007 11:12 AM by trickyvail

    Howto handle LazyInitializationException

      Hello

      I have a problem with lazy loading and need a workaround.

      I have an Entity called SoftwareInventoryObject which contains a Collection of SoftwareInventoryObjectParts.

      class SoftwareInventoryObject {
       // other fields
       List<SoftwareInventoryObjectPart> parts;
      
       // Getter & Setter
      }
      


      Additionally I have a EJB3 Bean SoftwareObjectSearch which searchs in the database for the above mentioned SoftwareInventoryObjects.
      class SoftwareObjectSearch {
       // other code
      
       @DataModel
       List<SoftwareObjectPart> inventoryObjects;
      
       // other code
      }
      


      To view the result I have a facelet which contains a <h:dataTable>. Now I would like to display each of the SoftwareObjectParts too therefor I have a nested <h:dataTable> in the facelet.
      <h:dataTable value="#{inventoryObjects}" var="sio" rendered="#{inventoryObjects != null}">
       <h:column>
       <h:dataTable value="#{sio.parts}" var="siop">
       <h:column><f:facet name="header">Name</f:facet> #{sio.name}</h:column>
       <h:column><f:facet name="header">Type</f:facet> #{sio.type}</h:column>
       <h:column><f:facet name="header">Barcode</f:facet> #{sio.barcode}</h:column>
       <h:column><f:facet name="header">Manufacture</f:facet> #{sio.manufacture}</h:column>
       <h:column><f:facet name="header">Distributor</f:facet> #{siop.distributor}</h:column>
       <h:column><f:facet name="header">Order Number</f:facet> #{siop.orderNumber}</h:column>
       <h:column><f:facet name="header">Price</f:facet> #{siop.price}</h:column>
       <h:column><f:facet name="header">Version</f:facet> #{siop.version}</h:column>
       <h:column><f:facet name="header">Action</f:facet> link</h:column>
       </h:dataTable>
       </h:column>
      </h:dataTable>
      


      Now when I run a facelet I get a Lazy Initalization Exception for the siop, the SoftwareInventoryObjectPart. This makes sense to me, because I access a collection which is not eager loaded, after the initial Hibernate session was closed. My question is now, how can I avoid such an exception? What is a nice workaround.

      If you need any further code please let me know. Thanks for all the answers in advance.

      Thierry