title: Generate data class attribute value only during creation
body: <p>I have a data class <code>Supplier</code> defined in my models:</p>
  
  <pre><code>data class Supplier (
          val id: Int = 0,
          val name: String,
          val owner: String,
  ) {
      companion object {
          private const val prefix = "SUP"
  
          private fun generateId(): String {
              return prefix + UUID.randomUUID().toString()
          }
      }
  
      val supplierId = generateId()
  }
  </code></pre>
  
  <p>I have a generated <code>supplierId</code> to generate a new identifier for each supplier that is created.</p>
  
  <p>Once I create a new <code>Supplier</code> and save to a database, the <code>supplierId</code> is stored.</p>
  
  <hr>
  
  <p>The issue is when I want to query a Supplier object based on an id provided by the user. The Dao implementation of the query function:</p>
  
  <pre><code>@SqlQuery("select $SUPPLIER_COLS from $TABLE_NAME where supplier_id = :supplier_id")
  fun getSupplier(@Bind("supplier_id") supplierId: String): Supplier?
  </code></pre>
  
  <p>This function gets the correct row from the database, but when it returns the object by generating one again, a new <code>supplierId</code> is generated as part of it.</p>
  
  <hr>
  
  <p>How do I modify my implementation such that the generator works only for the first time and not when reading from the database.</p>
  
  <p>One way I can think of it is to generate the id before creating the object and pass that as a parameter. Doing this would mean that my class signature will not immediately tell me what is a generated field and what is not. Is there any other approach I can take?</p>