title: How to use FetchType.LAZY with ManyToMany?
body: <p>I have two entities with many-to-many relationship.</p>
  
  <p>in class Role:</p>
  
  <pre><code>@ManyToMany(mappedBy = "roles")
  private Set&lt;User&gt; users = new HashSet&lt;&gt;();
  </code></pre>
  
  <p>and in class User:</p>
  
  <pre><code>@ManyToMany
  @JoinTable(name = "role_user", joinColumns = @JoinColumn(name = "user_id"),
          inverseJoinColumns = @JoinColumn(name = "role_id"))
  private Set&lt;Role&gt; roles = new HashSet&lt;&gt;();
  </code></pre>
  
  <p>And I get the exception: </p>
  
  <blockquote>
    <p>org.hibernate.LazyInitializationException: failed to lazily initialize
    a collection of role: User.roles, could not initialize proxy - no Session</p>
  </blockquote>
  
  <p>When I add <code>fetch = FetchType.EAGER</code>, I get another exception:</p>
  
  <blockquote>
    <p>java.lang.StackOverflowError: null</p>
  </blockquote>
  
  <p>and cycle between Role and User.</p>
  
  <p>How can I resolve this problem? I saw similar questions on Stackoverflow, but I didn't find real worked solution for me.</p>
  
  <p>UPD:
  Where the exception gets thrown:</p>
  
  <pre><code>@Service
  public class UserAuthenticationProvider implements AuthenticationProvider {
      ...
  
      @Override
      @Transactional
      public Authentication authenticate(final Authentication authentication) {
          final String login = authentication.getName();
          final String password = authentication.getCredentials().toString();
  
          final User user = userRepository.findByLogin(login);
          if (user != null &amp;&amp; passwordEncoder.matches(password, user.getPassword())) {
              return new UsernamePasswordAuthenticationToken(login, password,
                      user.getRoles()); // problem here
          } else {
              return null;
          }
      }
  
      ...
  </code></pre>