3 Replies Latest reply on Jan 15, 2010 9:28 AM by lvdberg

    Paginator and LazyInitializationException

    agori

      Look at this use case:


      ...
      @Scope(SESSION)
      class UserList {
        @DataModel List<User> users;
        @Factory("users") void loadUsers() {
          users = em.createQuery(...).getResultList();
        } 
      }
      



      <h:dataTable value="#{users}" var="user" rows="10">
        <h:column>#{user.roles}</h:column>
        ... 
      </h:dataTable>
      <rich:datascroller .../>
      



      If I have 20 users, then first page is ok. If I click to next I get an hibernate exception: LazyInitializationException.


      This happens because my entities are detached and the roles collection can't be initialized.


      The first solution is to loop on collection an initialize every entity:


      @Factory("users") void loadUsers() {
        users = em.createQuery(...).getResultList();
        for (User u : users) {
          Hibernate.initialize(u.getRoles());
        }
      } 
      



      But if I have 10000 users I don't want to do this operation for every user. It kills performance.


      So what is the best solution in this scenario? Note that I don't want to change UserList scope. Its scope is session and  not conversation or view.