title: Hibernate: failed to lazily initialize a collection
body: <p>I am getting the below error: when I am trying to save my data.</p>
  
  <blockquote>
    <p>org.hibernate.LazyInitializationException: failed to lazily initialize
    a collection of role: com.test.model.User.userRole, could not
    initialize proxy - no Session</p>
  </blockquote>
  
  <p>I have listed the classes that I have used.</p>
  
  <p>A User.java:</p>
  
  <pre><code>import java.util.HashSet;
  import java.util.Set;
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.FetchType;
  import javax.persistence.Id;
  import javax.persistence.OneToMany;
  import javax.persistence.Table;
  
  @Entity
  @Table(name = "users", catalog = "test")
  public class User {
  
  private String username;
  private String password;
  private boolean enabled;
  private Set&lt;UserRole&gt; userRole = new HashSet&lt;UserRole&gt;(0);
  
  public User() {
  }
  
  public User(String username, String password, boolean enabled) {
      this.username = username;
      this.password = password;
      this.enabled = enabled;
  }
  
  public User(String username, String password, 
      boolean enabled, Set&lt;UserRole&gt; userRole) {
      this.username = username;
      this.password = password;
      this.enabled = enabled;
      this.userRole = userRole;
  }
  
  @Id
  @Column(name = "username", unique = true, 
      nullable = false, length = 45)
  public String getUsername() {
      return this.username;
  }
  
  public void setUsername(String username) {
      this.username = username;
  }
  
  @Column(name = "password", 
      nullable = false, length = 60)
  public String getPassword() {
      return this.password;
  }
  
  public void setPassword(String password) {
      this.password = password;
  }
  
  @Column(name = "enabled", nullable = false)
  public boolean isEnabled() {
      return this.enabled;
  }
  
  public void setEnabled(boolean enabled) {
      this.enabled = enabled;
  }
  
  @OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
  public Set&lt;UserRole&gt; getUserRole() {
      return this.userRole;
  }
  
  public void setUserRole(Set&lt;UserRole&gt; userRole) {
      this.userRole = userRole;
  }
  
  }
  </code></pre>
  
  <p>And a UserRole.java:</p>
  
  <pre><code>import static javax.persistence.GenerationType.IDENTITY;
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.FetchType;
  import javax.persistence.GeneratedValue;
  import javax.persistence.Id;
  import javax.persistence.JoinColumn;
  import javax.persistence.ManyToOne;
  import javax.persistence.Table;
  import javax.persistence.UniqueConstraint;
  
  @Entity
  @Table(name = "user_roles", catalog = "test", uniqueConstraints =  UniqueConstraint(columnNames = { "role", "username" }))
  public class UserRole{
  
  private Integer userRoleId;
  private User user;
  private String role;
  
  public UserRole() {
  }
  
  public UserRole(User user, String role) {
      this.user = user;
      this.role = role;
  }
  
  @Id
  @GeneratedValue(strategy = IDENTITY)
  @Column(name = "user_role_id", 
      unique = true, nullable = false)
  public Integer getUserRoleId() {
      return this.userRoleId;
  }
  
  public void setUserRoleId(Integer userRoleId) {
      this.userRoleId = userRoleId;
  }
  
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "username", nullable = false)
  public User getUser() {
      return this.user;
  }
  
  public void setUser(User user) {
      this.user = user;
  }
  
  @Column(name = "role", nullable = false, length = 45)
  public String getRole() {
      return this.role;
  }
  
  public void setRole(String role) {
      this.role = role;
  }
  
  }
  </code></pre>
  
  <p>And I have a Dao Implemented:</p>
  
  <pre><code>@Transactional
  @Override
  @SuppressWarnings("unchecked")
  public List&lt;User&gt; listUsers() {
      return this.sessionFactory.getCurrentSession()
              .createCriteria(User.class).list();
  }
  </code></pre>
  
  <p>I am listing all users in my controller with the "listUsers" method. And add with model.addObject("users", listUsers);.</p>
  
  <p>In the view, i use the following code:</p>
  
  <pre><code>&lt;c:forEach items="${users}" var="user"&gt;
      Username: ${user.username}
      Pass:     ${user.password}
      Role:     ${user.userRole}
  &lt;/c:foreEach&gt;
  </code></pre>
  
  <p>But I got the above error (because of the ${user.userRole} variable):</p>
  
  <p>So how can I print out the role of the users (like: normal user, admin)?</p>