-
-
Save mchlstckl/4f9602b5d776878f48f0 to your computer and use it in GitHub Desktop.
// 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 = "") |
Works like a charm 🍸
Thank you
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
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 @Embeddable
to the data class to get it to work.
@Embeddable
data class PropertyId(
val uuid: UUID,
val name: String) : Serializable
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
@Embeddable
to the data class to get it to work.@Embeddable data class PropertyId( val uuid: UUID, val name: String) : Serializable
Thanks a lot this was solving our problems!
Thanks a lot for pointing to @IdClass
and @Id
annotations!
I tried using @EmbeddedId
first and couldn't get rid of "org.springframework.orm.jpa.JpaSystemException: Could not set field value [] value by reflection : " error.
Thank you, this was very helpful. Glad I found dug to the 2nd page of google and found this.