2 Replies Latest reply on Jan 21, 2007 10:00 PM by Gavin King

    Lazy loading problem

    Simon Tian Newbie

      I have an SFSB with name "invoiceListAction" in session scope. An entity manager with extended persistence context type is injected. When within a dataTable, I use following code to call a method of invoiceListAction. The lazy loading works well.

       <s:link styleClass="link_font" action="#{invoiceListAction.exportToPdf}">
       <t:graphicImage url="/media/pdf-icon.gif" />
       </s:link>
      

      The method "exportToPdf" calls anthor method "getSelectedInvoice" that annotated by @DataModelSelection at invoiceListAction to get an invoice instance. The address property of agency is in lazy fetch type.

      But if not within a dataTable, I used two ways to access the lazy loaded property, exception raised as "failed to lazily initialize a collection of role".
      One way is using exactly the same code as above. And the other way is as below:
      <c:set var="add" value="#{invoiceListAction.selectedInvoice.agency.primaryAddress}" scope="page" />
      


      Could anybody help me for this?

        • 1. Re: Lazy loading problem
          Simon Tian Newbie

          By further probing my code, I found it is actually caused by em.clear at another component. i.e. when I select a row at dataTable, the following code is executed:

          InvoiceContacts invoiceContactsAction = (InvoiceContacts) Component.getInstance("invoiceContactsAction",
           ScopeType.SESSION);
          invoiceContactsAction.changeInvoice(getSelectedInvoice());
          


          And code at component invoiceContactsAction is below:
          @Stateful
          @Name("invoiceContactsAction")
          @Scope(ScopeType.SESSION)
          public class InvoiceContactsAction implements InvoiceContacts {
          
           @PersistenceContext
           EntityManager em;
          
           ...
          
           public void changeInvoice(Invoice selectedInvoice) {
           this.selectedInvoice = selectedInvoice;
           tempList = null;
           invContacts = null;
           em.clear();
           }
          
           ...
          }
          

          So the question turn out to be: Why does clearing the entity manager at a component make the entity bean retrieved by another component detached under this situation?

          • 2. Re: Lazy loading problem
            Gavin King Master

            because there is a single persistence context for the whole transaction