Extending the Kotlin Data Class for use with JPA?

狂风中的少年 提交于 2019-12-24 19:28:08

问题


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)?

来源:https://stackoverflow.com/questions/46838256/extending-the-kotlin-data-class-for-use-with-jpa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!