Example entity with composite key Spring Boot and Kotlin
// This will not serialize! | |
data class Muppet(val name: String) | |
// This will serialize! | |
data class Puppet(val name: String = "") | |
// Composite key class must implement Serializable | |
// and have defaults. | |
class PropertyId( | |
val uuid: UUID = UUID.randomUUID(), | |
val name: String = "") : Serializable | |
// Need defaults everywhere! | |
@Entity | |
@IdClass(PropertyId::class) | |
data class Property( | |
@Id val uuid: UUID = UUID.randomUUID(), | |
@Id val name: String = "", | |
val value: String = "") |
This comment has been minimized.
This comment has been minimized.
Works like a charm |
This comment has been minimized.
This comment has been minimized.
I almost did set all those awful default values like in your example - but then I found the official Kotlin Docs recommending the JPA compiler plugin which generates the no-arg constructors for you! I think THAT is the way to go and worked perfectly fine for me: https://kotlinlang.org/docs/reference/compiler-plugins.html#jpa-support |
This comment has been minimized.
This comment has been minimized.
To add to what @mineralf said, the Kotlin JPA plugin needs a small hint to create that no-arg constructor. I found that I needed to add
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
Thank you, this was very helpful. Glad I found dug to the 2nd page of google and found this.