title: Hibernate roles and privileges
body: <p>I am trying to implement roles and privileges. But I get a session error message:</p>
  <p><strong>Message Request processing failed; nested exception is org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.testmany.shop.entity.AuthoritiesEntity.privilegesEntities, could not initialize proxy - no Session</strong></p>
  <p>Please tell me what am I doing wrong? I have not found a solution on the Internet. I cannot solve this problem for two days.</p>
  <p><a href="https://github.com/MyTestPerson/TestMany" rel="nofollow noreferrer">https://github.com/MyTestPerson/TestMany</a></p>
  <p><strong>This is how I create a user..</strong></p>
  <pre><code>    PrivilegesEntity privilegesEntity = serviceJpa.findPrivilegesEntityByPrivilegeEnum(&quot;PRIVILEGE_EDIT&quot;);
      AuthoritiesEntity authoritiesEntity = serviceJpa.findAuthoritiesEntityByRoleEnum(&quot;ROLE_USER&quot;);
  
      privilegesEntity.addAuthority(authoritiesEntity);
      authoritiesEntity.addUser(userEntity);
  
      serviceJpa.savePrivilegesEntity(privilegesEntity);
  </code></pre>
  <p><a href="https://i.stack.imgur.com/qwaU8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qwaU8.png" alt="enter image description here" /></a></p>
  <p><strong>UserEntity.class</strong></p>
  <pre><code>    @Entity(name = &quot;UserEntity&quot;)
      @Table(name = &quot;users&quot;, schema = &quot;test&quot;)
      public class UserEntity implements Serializable {
  
  
  
          private Long id;
          private String email;
          private String password;
          private Collection&lt;AuthoritiesEntity&gt; authoritiesEntities = new ArrayList&lt;&gt;();
  
  
  
  
          // id
          @Id
          @GeneratedValue(strategy = GenerationType.IDENTITY)
          @Column(name = &quot;id&quot;, nullable = false, unique = true)
          public Long getId() {
              return id;
          }
  
          public void setId(Long id) {
              this.id = id;
          }
  
  
  
  
          // email
          @NaturalId
          @Column(name = &quot;email&quot;, nullable = false, unique = true, length = 45)
          public String getEmail() {
              return email;
          }
  
          public void setEmail(String email) {
              this.email = email;
          }
  
  
  
  
          // password
          @Column(name = &quot;password&quot;, nullable = false, length = 65)
          public String getPassword() {
              return password;
          }
  
          public void setPassword(String password) {
              this.password = password;
          }
  
  
  
  
  
          @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
          public Collection&lt;AuthoritiesEntity&gt; getAuthoritiesEntities() {
              return authoritiesEntities;
          }
  
          public void setAuthoritiesEntities(Collection&lt;AuthoritiesEntity&gt; authoritiesEntities) {
              this.authoritiesEntities = authoritiesEntities;
          }
  
  
  
      }
  </code></pre>
  <p><strong>AuthoritiesEntity.class</strong></p>
  <pre><code>    @Entity(name = &quot;AuthoritiesEntity&quot;)
      @Table(name = &quot;authorities&quot;, schema = &quot;testmany&quot;)
      public class AuthoritiesEntity implements Serializable{
  
          private Long id;
          private String authority;
          private Collection&lt;UserEntity&gt; userEntityList = new ArrayList&lt;&gt;();
          private Collection&lt;PrivilegesEntity&gt; privilegesEntities = new ArrayList&lt;&gt;();
  
  
  
  
          // id
          @Id
          @GeneratedValue(strategy = GenerationType.IDENTITY)
          @Column(name = &quot;id&quot;, nullable = false, unique = true)
          public Long getId() {
              return id;
          }
  
          public void setId(Long id) {
              this.id = id;
          }
  
  
  
  
  
          // role
          @Column(name = &quot;authority&quot;, nullable = false, unique = true, length = 45, insertable = false, updatable = false)
          public String getAuthority() {
              return authority;
          }
  
          public void setAuthority(String authority) {
              this.authority = authority;
          }
  
  
  
  
  
          
  
          @ManyToMany(mappedBy = &quot;authoritiesEntities&quot;, cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER)
          public Collection&lt;UserEntity&gt; getUserEntityList() {
              return userEntityList;
          }
  
          public void setUserEntityList(Collection&lt;UserEntity&gt; userEntityList) {
              this.userEntityList = userEntityList;
          }
  
  
  
  
  
          @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
          public Collection&lt;PrivilegesEntity&gt; getPrivilegesEntities() {
              return privilegesEntities;
          }
  
          public void setPrivilegesEntities(Collection&lt;PrivilegesEntity&gt; privilegesEntities) {
              this.privilegesEntities = privilegesEntities;
          }
  
  
  
  
  
  
          // addUser
          public void addUser(UserEntity userEntity) {
              userEntityList.add( userEntity );
              userEntity.getAuthoritiesEntities().add( this );
          }
  
  
  
  
  
      }
  </code></pre>
  <p><strong>PrivilegesEntity.class</strong></p>
  <pre><code>    package com.testmany.shop.entity;
  
      import com.testmany.shop.enam.PrivilegeEnum;
  
      import javax.persistence.*;
      import java.io.Serializable;
      import java.util.ArrayList;
      import java.util.Collection;
      import java.util.List;
  
      @Entity(name = &quot;PrivilegesEntity&quot;)
      @Table(name = &quot;privileges&quot;, schema = &quot;testmany&quot;)
      public class PrivilegesEntity implements Serializable {
  
  
  
          private Long id;
          private String privilege;
          private Collection&lt;AuthoritiesEntity&gt; authoritiesEntityList = new ArrayList&lt;&gt;();
  
  
  
  
          @Id
          @GeneratedValue(strategy = GenerationType.IDENTITY)
          @Column(name = &quot;id&quot;, nullable = false, unique = true)
          public Long getId() {
              return id;
          }
  
          public void setId(Long id) {
              this.id = id;
          }
  
  
  
  
  
  
  
  
          @Column(name = &quot;privilege&quot;, nullable = false, unique = true, length = 45, insertable = false, updatable = false)
          public String getPrivilege() {
              return privilege;
          }
  
          public void setPrivilege(String privilege) {
              this.privilege = privilege;
          }
  
  
  
  
  
  
  
  
          @ManyToMany(mappedBy = &quot;privilegesEntities&quot;, cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER)
          public Collection&lt;AuthoritiesEntity&gt; getAuthoritiesEntityList() {
              return authoritiesEntityList;
          }
  
          public void setAuthoritiesEntityList(Collection&lt;AuthoritiesEntity&gt; authoritiesEntityList) {
              this.authoritiesEntityList = authoritiesEntityList;
          }
  
  
  
  
  
  
  
  
  
          // addAuthority
          public void addAuthority(AuthoritiesEntity authoritiesEntity) {
              authoritiesEntityList.add( authoritiesEntity );
              authoritiesEntity.getPrivilegesEntities().add( this );
          }
  
  
  
      }
  </code></pre>