title: Hibernate lazy initialization - failed to lazily initialize a collection
body: <p>I am getting the following error whenever I try to retrieve data from my Users table, I've searched a web a lot trying to find out what could be wrong but couldn't find anything so can someone please help me by telling me what I am missing / I have wrong here?</p>
  
  <p>Error:</p>
  
  <blockquote>
    <p>ERROR: org.hibernate.LazyInitializationException - failed to lazily
    initialize a collection of role: com.domain.crm.domain.Role.users, no
    session or session was closed
    org.hibernate.LazyInitializationException: failed to lazily initialize
    a collection of role: com.domain.crm.domain.Role.users, no session or
    session was closed</p>
  </blockquote>
  
  <p>User class:</p>
  
  <pre><code>@Entity
  @Table(name="COM_USER")
  public class User {
  @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  @Column(name="USER_ID")
  private Long id;
  
  @Column(name="USER_NAME",nullable=false,length=25,unique=true)
  private String userName;
  
  @ManyToOne(fetch=FetchType.EAGER)
  @JoinColumn(name="ROLE_ID",nullable=false)
  private Role role;
  }
  </code></pre>
  
  <p>Role Class:</p>
  
  <pre><code>@Entity
  @Table(name="COM_ROLE")
  public class Role {
  @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  @Column(name="ROLE_ID")
  private Long id;
  
  @Column(name="ROLE",nullable=false,unique=false)
  private Integer Role;
  
  @OneToMany(mappedBy="role")
  private Set&lt;User&gt; users=new HashSet&lt;User&gt;();
  }   
  </code></pre>
  
  <p>User DAO class Method being invoked to collect all users:</p>
  
  <pre><code>public List&lt;User&gt; getUsers(Long page, Long pageSize) {
      Long start = (page-1)*pageSize;     
      return sessionfactory.getCurrentSession().createQuery("from User u ").setFirstResult(start.intValue()).setMaxResults(pageSize.intValue()).list();
  }
  </code></pre>
  
  <p>User Service class method:</p>
  
  <pre><code>@Transactional
  public List&lt;User&gt; getUsers(Long page, Long pageSize) {
      return userdao.getUsers(page, pageSize);
  }
  </code></pre>
  
  <p>Controller class calling the method:</p>
  
  <pre><code>@RequestMapping(value = "/users/list-user-data")
  @ResponseBody
  public UserListData listUserData(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
  
      UserListData listData = new UserListData();
  
      String page = request.getParameter("page");
      Long pageLong = Long.parseLong(page);
      Long pageSize = (long)15;
  
      List&lt;User&gt; searchResults = iuserservice.getUsers(pageLong, pageSize);
  
      if( searchResults != null ){
          List&lt;List&lt;Object&gt;&gt; aaData = new ArrayList&lt;List&lt;Object&gt;&gt;();
          List&lt;Object&gt; listItem = null;
  
          for( User u : searchResults ){
              listItem = new ArrayList&lt;Object&gt;();
              listItem.add(u.getLastName());
              listItem.add(u.getFirstName());
              listItem.add(u.getUserName());
              listItem.add(u.getEmail());
              listItem.add(u.getRole());
  
              aaData.add(listItem);
          }
          listData.setAaData(aaData);
      }
  
  
  
      int totalCount = iuserservice.getAllUsersCount().intValue();
  
      System.out.println("Number of records in DB:  "+totalCount);
      listData.setiTotalRecords(totalCount);
  
      return listData;
  }
  </code></pre>
  
  <p>Finally here is my pom.xml dependencies:</p>
  
  <pre><code>&lt;properties&gt;
      &lt;java-version&gt;1.6&lt;/java-version&gt;
      &lt;org.springframework-version&gt;3.1.0.RELEASE&lt;/org.springframework-version&gt;
      &lt;org.aspectj-version&gt;1.6.9&lt;/org.aspectj-version&gt;
      &lt;org.slf4j-version&gt;1.5.10&lt;/org.slf4j-version&gt;
  &lt;/properties&gt;
  
  &lt;dependencies&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-context&lt;/artifactId&gt;
          &lt;version&gt;${org.springframework-version}&lt;/version&gt;
          &lt;exclusions&gt;
              &lt;!-- Exclude Commons Logging in favor of SLF4j --&gt;
              &lt;exclusion&gt;
                  &lt;groupId&gt;commons-logging&lt;/groupId&gt;
                  &lt;artifactId&gt;commons-logging&lt;/artifactId&gt;
               &lt;/exclusion&gt;
          &lt;/exclusions&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt;
          &lt;version&gt;${org.springframework-version}&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;!-- AspectJ --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.aspectj&lt;/groupId&gt;
          &lt;artifactId&gt;aspectjrt&lt;/artifactId&gt;
          &lt;version&gt;${org.aspectj-version}&lt;/version&gt;
      &lt;/dependency&gt;   
      &lt;!-- Logging --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
          &lt;artifactId&gt;slf4j-api&lt;/artifactId&gt;
          &lt;version&gt;${org.slf4j-version}&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
          &lt;artifactId&gt;jcl-over-slf4j&lt;/artifactId&gt;
          &lt;version&gt;${org.slf4j-version}&lt;/version&gt;
          &lt;scope&gt;runtime&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
          &lt;artifactId&gt;slf4j-log4j12&lt;/artifactId&gt;
          &lt;version&gt;${org.slf4j-version}&lt;/version&gt;
          &lt;scope&gt;runtime&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;log4j&lt;/groupId&gt;
          &lt;artifactId&gt;log4j&lt;/artifactId&gt;
          &lt;version&gt;1.2.15&lt;/version&gt;
          &lt;exclusions&gt;
              &lt;exclusion&gt;
                  &lt;groupId&gt;javax.mail&lt;/groupId&gt;
                  &lt;artifactId&gt;mail&lt;/artifactId&gt;
              &lt;/exclusion&gt;
              &lt;exclusion&gt;
                  &lt;groupId&gt;javax.jms&lt;/groupId&gt;
                  &lt;artifactId&gt;jms&lt;/artifactId&gt;
              &lt;/exclusion&gt;
              &lt;exclusion&gt;
                  &lt;groupId&gt;com.sun.jdmk&lt;/groupId&gt;
                  &lt;artifactId&gt;jmxtools&lt;/artifactId&gt;
              &lt;/exclusion&gt;
              &lt;exclusion&gt;
                  &lt;groupId&gt;com.sun.jmx&lt;/groupId&gt;
                  &lt;artifactId&gt;jmxri&lt;/artifactId&gt;
              &lt;/exclusion&gt;
          &lt;/exclusions&gt;
          &lt;scope&gt;runtime&lt;/scope&gt;
      &lt;/dependency&gt;
  
      &lt;!-- @Inject --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;javax.inject&lt;/groupId&gt;
          &lt;artifactId&gt;javax.inject&lt;/artifactId&gt;
          &lt;version&gt;1&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- Servlet --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
          &lt;artifactId&gt;servlet-api&lt;/artifactId&gt;
          &lt;version&gt;2.5&lt;/version&gt;
          &lt;scope&gt;provided&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;javax.servlet.jsp&lt;/groupId&gt;
          &lt;artifactId&gt;jsp-api&lt;/artifactId&gt;
          &lt;version&gt;2.1&lt;/version&gt;
          &lt;scope&gt;provided&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
          &lt;artifactId&gt;jstl&lt;/artifactId&gt;
          &lt;version&gt;1.2&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;!-- Test --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;junit&lt;/groupId&gt;
          &lt;artifactId&gt;junit&lt;/artifactId&gt;
          &lt;version&gt;4.7&lt;/version&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;        
      &lt;!-- Spring Security --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
          &lt;artifactId&gt;spring-security-core&lt;/artifactId&gt;
          &lt;version&gt;3.1.3.RELEASE&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
          &lt;artifactId&gt;spring-security-web&lt;/artifactId&gt;
          &lt;version&gt;3.1.3.RELEASE&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
          &lt;artifactId&gt;spring-security-config&lt;/artifactId&gt;
          &lt;version&gt;3.1.3.RELEASE&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
          &lt;artifactId&gt;spring-security-taglibs&lt;/artifactId&gt;
          &lt;version&gt;3.1.3.RELEASE&lt;/version&gt;
          &lt;type&gt;jar&lt;/type&gt;
          &lt;scope&gt;compile&lt;/scope&gt;
      &lt;/dependency&gt;       
      &lt;!-- Mysql --&gt;  
      &lt;dependency&gt;
          &lt;groupId&gt;mysql&lt;/groupId&gt;
          &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
          &lt;version&gt;5.1.6&lt;/version&gt;
      &lt;/dependency&gt;           
      &lt;!-- Hibernate --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
          &lt;artifactId&gt;hibernate-entitymanager&lt;/artifactId&gt;
          &lt;version&gt;3.6.0.Final&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-orm&lt;/artifactId&gt;
          &lt;version&gt;${org.springframework-version}&lt;/version&gt;
      &lt;/dependency&gt;       
      &lt;!-- Commons DBCP --&gt;   
      &lt;dependency&gt;
          &lt;groupId&gt;commons-dbcp&lt;/groupId&gt;
          &lt;artifactId&gt;commons-dbcp&lt;/artifactId&gt;
          &lt;version&gt;1.2.2&lt;/version&gt;
      &lt;/dependency&gt;   
      &lt;dependency&gt;
          &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
          &lt;artifactId&gt;maven-resources-plugin&lt;/artifactId&gt;
          &lt;version&gt;2.4.3&lt;/version&gt;
      &lt;/dependency&gt;   
      &lt;dependency&gt;
          &lt;groupId&gt;net.sf.jasperreports&lt;/groupId&gt;
          &lt;artifactId&gt;jasperreports&lt;/artifactId&gt;
          &lt;version&gt;5.2.0&lt;/version&gt;
      &lt;/dependency&gt;    
      &lt;dependency&gt;
          &lt;groupId&gt;commons-collections&lt;/groupId&gt;
          &lt;artifactId&gt;commons-collections&lt;/artifactId&gt;
          &lt;version&gt;3.1&lt;/version&gt;
      &lt;/dependency&gt;           
  &lt;/dependencies&gt;
  </code></pre>
  
  <p>Thanks for your time</p>