title: Kotlin with JPA: default constructor hell
body: <p>As JPA requires, <code>@Entity</code> classes should have a default (non-arg) constructor to instantiate the objects when retrieving them from the database.</p>
  
  <p>In Kotlin, properties are very convenient to declare within the primary constructor, as in the following example:</p>
  
  <pre><code>class Person(val name: String, val age: Int) { /* ... */ }
  </code></pre>
  
  <p>But when the non-arg constructor is declared as a secondary one it requires values for the primary constructor to be passed, so some valid values are needed for them, like here:</p>
  
  <pre><code>@Entity
  class Person(val name: String, val age: Int) {
      private constructor(): this("", 0)
  }
  </code></pre>
  
  <p>In case when the properties have some more complex type than just <code>String</code> and <code>Int</code> and they're non-nullable, it looks totally bad to provide the values for them, especially when there's much code in primary constructor and <code>init</code> blocks and when the parameters are actively used -- when they're to be reassigned through reflection most of the code is going to be executed again.</p>
  
  <p>Moreover, <code>val</code>-properties cannot be reassigned after the constructor executes, so immutability is also lost.</p>
  
  <p>So the question is: how can Kotlin code be adapted to work with JPA without code duplication, choosing "magic" initial values and loss of immutability?</p>
  
  <p>P.S. Is it true that Hibernate aside of JPA can construct objects with no default constructor?</p>