0

Kotlin has a Data class that automatically implements equals and hashcode, but these are still not automatically usable in a JPA context.

In order to remedy this I was wondering what it would take to extend the Data type in order to either assign a "Business Key" or an id property that is final and non updatable and is initialized with a UUID that serves as the business key, and have equals() and hashcode() methods use that property for their implementation.

I was thinking roughly something like this:

jpa class User(id: JpaId, val name: String, val age: Int)

jpa class User(bid: JpaBid, val name: String, val age: Int)

Instead of using a Data class like this:

data class User(bid: long, val name: String, val age: Int)

We are using a new type of class jpa. We also have two new types, JpaId and JpaBid that are used to determine whether the id property should be a UUID or a business key, like an ISBN number, sku, etc.

If a JpaId type is used then the key is automatically generated. If a JpaBid is used then the key should be supplied in the constructor. This would ensure that as soon as a JPA entity is constructed, it can be uniquely identified. Thoughts? Is this something that is very straight forward to do in Kotlin, or do we need to get up in the JVM grill (Brand new to Kotlin)?

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
Ole
  • 41,793
  • 59
  • 191
  • 359
  • This would be a feature proposal for Kotlin, not something you could easily do yourself. – Robin Oct 20 '17 at 13:10
  • It's too bad Kotlin Github issues are disabled. I would put a feature request in. – Ole Oct 21 '17 at 04:58
  • 1
    have a look at the Kotlin Evolution and Enhancement Process repository, that's where all these things are discussed and you can put in new proposals via pull request: https://github.com/Kotlin/KEEP – Robin Oct 21 '17 at 06:33
  • Super - Thanks for the heads up! – Ole Oct 21 '17 at 07:00
  • https://github.com/Kotlin/KEEP/issues/93 – Ole Oct 28 '17 at 19:43

0 Answers0