Search code examples
hibernatekotlinjpa

Best way to inherit tables with kotlin and jpa


What is the best way to inherit jpa entity using kotlin
For example i have abstract entity User which looks like

@Entity
@Table(name = "users")
@Inheritance(strategy = InheritanceType.JOINED)
abstract class User (
  val password: String,
  var email: String
)

And i got second entity PrivateUser extended from User

@Entity
@Table(name = "private_person")
class PrivatePerson(
    override var password: String,
    override var email: String,
    val name: String
) : User(password, email)   

Actually i don't like to override every single property from super class. Maybe there's more elegant solution for that task.
Or is it the best way to work around


Solution

  • Good question.

    Maybe you need something like that?

    abstract class User (
            open val password: String = "",
            open var email: String = ""
        )
    
    class PrivatePerson(
            override var password: String,
            val name: String
        ): User(password)  
    
    

    You have to have an initialized variable if you want to skip it.