title: JPA LazyInitialisationException - Could Not Initialize Proxy
body: <p>Yes I've seen similar questions, but none of the answers actually led me to a solution. I'm not using the thread anywhere, and this same code works in another jhipster app, so I'm stumped why this lift+shift is causing Hibernate problems.</p>
  
  <pre><code>public UserDetailsContextMapper userDetailsContextMapper() {
      return new UserDetailsContextMapper() {
          @Override
          public UserDetails mapUserFromContext(
              DirContextOperations ctx, String username,
              Collection&lt;? extends GrantedAuthority&gt; authorities) {
              log.error("2 " + username + " -&gt; " + ctx.toString());
              String lowercaseLogonName = username.toLowerCase();
              Optional&lt;User&gt; userFromDatabase = userRepository.findOneByLogin(lowercaseLogonName);
              if (!userFromDatabase.isPresent()) {
                  User ldapUser = new User();
                  ldapUser.setLogin(lowercaseLogonName);
                  ldapUser.setPassword(RandomStringUtils.random(60)); // We use LDAP password, but the password need to be set
                  ldapUser.setActivated(true);
                  try {
                      Attribute attribute = ctx.getAttributes().get("mail");
                      ldapUser.setEmail( attribute != null &amp;&amp; attribute.size() &gt; 0 ? (String) attribute.get(0) : "");
                      attribute = ctx.getAttributes().get("givenname");
                      ldapUser.setFirstName(attribute != null &amp;&amp; attribute.size() &gt; 0 ? (String) attribute.get(0) : "");
                      attribute = ctx.getAttributes().get("sn");
                      ldapUser.setLastName(attribute != null &amp;&amp; attribute.size() &gt; 0 ? (String) attribute.get(0) : "");
                  } catch (NamingException e) {
                      log.warn("Couldn't get LDAP details for user: " + lowercaseLogonName);
                  }
  
                  Set&lt;Authority&gt; newUserAuthorities = new HashSet&lt;&gt;();
                  Authority authority = authorityRepository.getOne(AuthoritiesConstants.USER);
  
                  newUserAuthorities.add(authority); &lt;-- This line in the Exception
  
                  ldapUser.setAuthorities(newUserAuthorities);
                  ldapUser.setLangKey("en");
                  userRepository.saveAndFlush(ldapUser);
  
              }
  </code></pre>
  
  <p>Exception: </p>
  
  <pre><code>org.hibernate.LazyInitializationException: could not initialize proxy [com.app.my.let.domain.Authority#ROLE_USER] - no Session
          at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:155)
          at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:268)
          at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:73)
          at com.app.my.let.domain.Authority_$$_jvst758_1e.hashCode(Authority_$$_jvst758_1e.java)
          at java.util.HashMap.hash(HashMap.java:339)
          at java.util.HashMap.put(HashMap.java:612)
          at java.util.HashSet.add(HashSet.java:220)
          at com.app.my.let.config.SecurityConfiguration$1.mapUserFromContext(SecurityConfiguration.java:249) &lt;-- code line above
  </code></pre>
  
  <p>JPA properties:</p>
  
  <pre><code>jpa:
          database-platform: org.hibernate.dialect.SQLServer2012Dialect
          database: SQL_SERVER
          show_sql: true
          format_sql: true
          properties:
              hibernate.id.new_generator_mappings: true
              hibernate.cache.use_second_level_cache: false
              hibernate.cache.use_query_cache: false
              hibernate.generate_statistics: true
  </code></pre>
  
  <p><strong>EDIT - This resolved my issue:</strong>  </p>
  
  <pre><code>Optional&lt;Authority&gt; optAuthority = authorityRepository.findById(AuthoritiesConstants.USER);
  
  Authority authority = optAuthority.get();
  </code></pre>