title: Kotlin spring JPA. lateinit property has not been initialized exception from getting lazy field of JPA entity
body: <p>I use Spring Boot, Hibernate, Kotlin</p>
  <p>In build.gradle.kts:</p>
  <pre><code>apply(plugin=&quot;org.hibernate.orm&quot;)
  
  tasks.withType&lt;org.hibernate.orm.tooling.gradle.EnhanceTask&gt;().configureEach {
      options.enableLazyInitialization = true
      options.enableDirtyTracking = true
      options.enableAssociationManagement = true
  }
  </code></pre>
  <p>User entity:</p>
  <pre><code>@Entity
      @Table(name = &quot;user&quot;)
      class User(
              @Id
              @GeneratedValue(strategy = GenerationType.AUTO)
              @Column(name = &quot;id&quot;)
              var id: Long = -1,    
      
              @Column(name = &quot;user_name&quot;, unique = true, nullable = false, length = 20)
              var userName: String = &quot;&quot;,
      
      
              @Column(unique = true, nullable = false)
              var email: String = &quot;&quot;,
  
              @ManyToMany(fetch = FetchType.EAGER)
              @JoinTable(name = &quot;user_roles&quot;, joinColumns = [JoinColumn(name = &quot;user_id&quot;)], 
               inverseJoinColumns = [JoinColumn(name = &quot;role_id&quot;)])
               var roles: MutableSet&lt;Role&gt; = mutableSetOf()
      
      
      ) {
          @OneToOne(fetch = FetchType.LAZY, cascade = [CascadeType.ALL], mappedBy = &quot;user&quot;, optional = false)
          @LazyToOne(LazyToOneOption.PROXY)
          @LazyGroup( &quot;person&quot; )
          lateinit var person: Person
      ....
      
      }
  </code></pre>
  <p>I get it by:</p>
  <pre><code>@Transactional
  @Component
  class UserServiceImpl (private val userRepository: UserRepository){
  
      
      override fun getUserData(id: Long): Optional&lt;UserView&gt; {
        return  userRepository.findById(id).map { UserView.build(it, it.person) }
  
      }
  ...
  
  }
  </code></pre>
  <p>And it.person throws <code>lateinit property has not been initialized exception</code>, but Person was loaded( I see it by hibernate log ). Getting roles works fine.</p>
  <p>I have same result without  @LazyToOne(LazyToOneOption.PROXY) and  @LazyGroup( &quot;person&quot; ).</p>
  <hr />
  <p>SOLVED:</p>
  <pre><code>@OneToOne(fetch = FetchType.LAZY, cascade = [CascadeType.ALL])
      @JoinColumn(name = &quot;person_id&quot;, referencedColumnName = &quot;id&quot;)
      @LazyToOne(LazyToOneOption.PROXY)
      @LazyGroup( &quot;person&quot; )
     lateinit var person: Person
  </code></pre>