2 Replies Latest reply on Aug 24, 2010 9:28 PM by petrik.mich

    Seam managed persistence context used in page scope bean + LIE

    petrik.mich

      Hi,
      hope that someone can help me with this.


      I would like to try the feature of Seam managed persistence context (as I understand from documentation) that it can be spanned across multiple request without need to reload data from db to synchronize them with context. But I can't get work even this simple scenario and always get the LazyEvaluationException. Probably I'm missing something crucial but I don't know what.


      This is the page scoped bean:


      @Name("userManagement")
      @Scope(ScopeType.PAGE)
      public class UserManagementBean implements Serializable{
           
           private static final long serialVersionUID = -20674003247424563L;
           
           @In
           EntityManager entityManager;
           
           @DataModel
           List<User> usersList;
           
           @DataModelSelection
           User selectedUser;
           
           private User detailedUser;
           
           private boolean detailRendered = false;
           
           @SuppressWarnings("unchecked")
           @Factory("usersList")
           public void initUsers(){
                usersList = entityManager.createNamedQuery("getUsers").getResultList();
           }
           
           public void showDetail(){
                detailedUser = selectedUser;
                detailRendered = true;
           }
      
              public boolean isDetailRendered() {
                return detailRendered;
           }
              public User getDetailedUser() {
                return detailedUser;
           }
      



      And this is the .xhtml page:


      I just want to show some details of User entity in the ajax rendered output panel when the link in data table is clicked. But the problem is that the User entity has lazy initialized collection of roles that I also want to display in that panel. And it leads to LIE.


      I thought if I use seam managed persistence context that once the entity is loaded it remains managed until the entity manger is detached at the end of the bean scope.




      <rich:panel id="cn" header="#{messages.users}">
                     <a4j:outputPanel id="udp" rendered="#{userManagement.detailRendered}">
                          <p><h:outputText
                               value="#{userManagement.detailedUser.commonName}" /></p>
                          <p><h:outputText value="#{userManagement.detailedUser.email}" /></p>
                          <br />
                          <br />
                          <h:outputText value="#{messages.memberOf}" />
                          <rich:separator id="sp1" />
                          <h:dataTable id="rol"
                               value="#{userManagement.detailedUser.rolesSorted}" var="role">
                               <h:column>
                                    <s:link id="pl" view="/#{links.roleProfile}" propagation="none"
                                         value="#{role.name}">
                                         <f:param name="roleName" value="#{role.name}" />
                                    </s:link>
                               </h:column>
                          </h:dataTable>
                     </a4j:outputPanel>
                     <h:form id="tfrm">
                          <rich:dataTable id="us" var="user" value="#{usersList}" rows="15">
                               <f:facet name="header" id="fh">
                                    <rich:columnGroup id="cg">
                                         <rich:column>
                                              <h:outputText value="#{messages.username}" />
                                         </rich:column>
                                         <rich:column id="hn">
                                              <h:outputText value="#{messages.name}" />
                                         </rich:column>
                                         <rich:column id="hp" />
                                    </rich:columnGroup>
                               </f:facet>
                               <rich:column id="u">
                                    <h:outputText value="#{user.username}" />
                               </rich:column>
                               <rich:column id="n">
                                    <h:outputText value="#{user.commonName}" />
                               </rich:column>
                               <rich:column id="p">
                                    <a4j:commandLink id="lpr" ajaxSingle="true"
                                         title="#{messages.details}" reRender="cn"
                                         action="#{userManagement.showDetail()}">
                                         <h:graphicImage id="pi" value="#{images.details}" />
                                    </a4j:commandLink>
                               </rich:column>
                          </rich:dataTable>
                          <rich:datascroller id="usscr" renderIfSinglePage="false"
                               maxPages="10" for="us" />
                     </h:form>
                </rich:panel>



      Thanks for reply.


      Michal