Last active
October 15, 2024 20:14
-
-
Save michaelst0/4f9602b5d776878f48f0 to your computer and use it in GitHub Desktop.
Example entity with composite key Spring Boot and Kotlin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 = "") |
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.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.