I'm trying to solve my inheritance problem with kotlin sealed class and forced problem with hibernate.
Here are my classes:
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING)
sealed class LegalGuardian(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
var id: Int? = null
)
@Entity
@DiscriminatorValue(value = "MOTHER")
data class MotherLegalGuardian(
@OneToOne(cascade = [CascadeType.ALL], orphanRemoval = true)
val pesel: Pesel
) : LegalGuardian()
@Entity
@DiscriminatorValue(value = "OTHER")
data class OtherLegalGuardian(
val firstName: String,
val lastName: String,
@OneToOne
val address: Address
) : LegalGuardian()
Here is poroblem that is thrown:
Caused by: org.hibernate.MappingException: Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:123) at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:77) at org.hibernate.internal.SessionFactoryImpl.(SessionFactoryImpl.java:348) at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:444) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:879) ... 95 common frames omitted Caused by: org.hibernate.InstantiationException: could not instantiate test object : X.MotherLegalGuardian at org.hibernate.engine.internal.UnsavedValueFactory.instantiate(UnsavedValueFactory.java:43) at org.hibernate.engine.internal.UnsavedValueFactory.getUnsavedIdentifierValue(UnsavedValueFactory.java:68) at org.hibernate.tuple.PropertyFactory.buildIdentifierAttribute(PropertyFactory.java:61) at org.hibernate.tuple.entity.EntityMetamodel.(EntityMetamodel.java:141) at org.hibernate.persister.entity.AbstractEntityPersister.(AbstractEntityPersister.java:517) at org.hibernate.persister.entity.SingleTableEntityPersister.(SingleTableEntityPersister.java:124) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:96) ... 99 common frames omitted Caused by: java.lang.reflect.InvocationTargetException: null at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at org.hibernate.engine.internal.UnsavedValueFactory.instantiate(UnsavedValueFactory.java:40) ... 109 common frames omitted Caused by: java.lang.NoSuchMethodError: X.LegalGuardian.(Lkotlin/jvm/internal/DefaultConstructorMarker;)V at X.MotherLegalGuardian.(LegalGuardian.kt) ... 114 common frames omitted