title: Kotlin Hibernate JPA Lazy fetch not working through the controller
body: <p>I've get a full sample application here: <a href="https://github.com/MrMojoR/hibernateOnKotlin" rel="nofollow noreferrer">https://github.com/MrMojoR/hibernateOnKotlin</a></p>
  
  <p>And this code is based on this blogpost: <a href="https://kotlinexpertise.com/hibernate-with-kotlin-spring-boot/" rel="nofollow noreferrer">https://kotlinexpertise.com/hibernate-with-kotlin-spring-boot/</a></p>
  
  <p>The problem is, that while lazy fetch works perfectly from the integration test, there is an Exception in debugger:
  <a href="https://i.stack.imgur.com/ltKWp.jpg" rel="nofollow noreferrer">Exception from test</a></p>
  
  <p>When I run the same code from the Controller, there is no Exception, the whole entity is loaded:
  <a href="https://i.stack.imgur.com/kMCrs.jpg" rel="nofollow noreferrer">No Exception from controller</a></p>
  
  <p><strong>How is that possible?</strong>
  Thanks very much for your help!</p>
  
  <p>I'll post the code snippets anyway:</p>
  
  <p>AbstractJpaPersistable.kt</p>
  
  <pre><code>import org.springframework.data.domain.Persistable
  import org.springframework.data.util.ProxyUtils
  import java.io.Serializable
  import javax.persistence.GeneratedValue
  import javax.persistence.Id
  import javax.persistence.MappedSuperclass
  import javax.persistence.Transient
  
  /**
   * Abstract base class for entities. Allows parameterization of id type, chooses auto-generation and implements
   * [equals] and [hashCode] based on that id.
   *
   * This class was inspired by [org.springframework.data.jpa.domain.AbstractPersistable], which is part of the Spring Data project.
   */
  @MappedSuperclass
  abstract class AbstractJpaPersistable&lt;T : Serializable&gt; : Persistable&lt;T&gt; {
  
      companion object {
          private val serialVersionUID = -5554308939380869754L
      }
  
      @Id
      @GeneratedValue
      private var id: T? = null
  
      override fun getId(): T? {
          return id
      }
  
      /**
       * Must be [Transient] in order to ensure that no JPA provider complains because of a missing setter.
       *
       * @see org.springframework.data.domain.Persistable.isNew
       */
      @Transient
      override fun isNew() = null == getId()
  
      override fun toString() = "Entity of type ${this.javaClass.name} with id: $id"
  
      override fun equals(other: Any?): Boolean {
          other ?: return false
  
          if (this === other) return true
  
          if (javaClass != ProxyUtils.getUserClass(other)) return false
  
          other as AbstractJpaPersistable&lt;*&gt;
  
          return if (null == this.getId()) false else this.getId() == other.getId()
      }
  
      override fun hashCode(): Int {
          return 31
      }
  }
  </code></pre>
  
  <p>Person.kt:</p>
  
  <pre><code>import javax.persistence.CascadeType
  import javax.persistence.Entity
  import javax.persistence.FetchType
  import javax.persistence.ManyToOne
  import javax.persistence.OneToMany    
  
  @Entity
  class Person(
          val name: String,
          @ManyToOne(cascade = [(CascadeType.ALL)], fetch = FetchType.EAGER)
          val street: Street
  ) : AbstractJpaPersistable&lt;Long&gt;()
  
  @Entity
  class Address(
          val zipCode: String,
          val city: String
  ) : AbstractJpaPersistable&lt;Long&gt;()
  
  @Entity
  class Street(
          @OneToMany(cascade = [(CascadeType.ALL)], fetch = FetchType.LAZY)
          val adresses: MutableSet&lt;Address&gt;
  ) : AbstractJpaPersistable&lt;Long&gt;()
  </code></pre>
  
  <p>PersonRepository:</p>
  
  <pre><code>import com.kotlinexpertise.hibernatedemo.model.Person
  import org.springframework.data.jpa.repository.JpaRepository
  
  interface PersonRepository : JpaRepository&lt;Person, Long&gt;
  </code></pre>
  
  <p>PersonService:</p>
  
  <pre><code>import com.kotlinexpertise.hibernatedemo.model.Person
  import com.kotlinexpertise.hibernatedemo.repository.PersonRepository
  import org.springframework.stereotype.Service
  
  @Service
  class PersonService(val personRepository: PersonRepository) {
  
      fun savePerson(person: Person) {
          personRepository.saveAndFlush(person)
      }
  }
  </code></pre>
  
  <p><strong>Solution:</strong></p>
  
  <p><a href="https://stackoverflow.com/questions/30549489/what-is-this-spring-jpa-open-in-view-true-property-in-spring-boot">What is this spring.jpa.open-in-view=true property in Spring Boot?</a></p>
  
  <p>This property should be set to false:</p>
  
  <pre><code>spring.jpa.open-in-view=false
  </code></pre>
  
  <p>It is not a Kotlin issue, but a Spring issue.</p>