Best practice for Hibernate


#1

I’m using Kotlin with Spring and Hibernate. What’s the best practice for Hibernate models with Kotlin? Should I use immutable or mutable classes? Should I use data class? Should I use nullable property for id? Should I use constructor properties or body properties? It seems that there’s absolutely no kotlin-specific support in Hibernate and I did not find popular helper libraries.

Currently I just started and the following class seems to work:

@Entity
class Account(
        @Id
        val id: Int?,

        var name: String,

        var password: String,

        @Enumerated(STRING)
        var type: AccountType
)

but the drawback is that I have to use !! every time I’m accessing id. On the other hand I must use something like null to indicate that hibernate must generate id on save.


#2

You can move id to the class body if you need it to be generated

@Entity
class Account(
//....
) {
   @Id @GeneratedValue
   val id = 0 // will be replaced by hibernate
}