@MappedSuperclass
public abstract class AbstractBaseModel{ }
@MappedSuperclass
public class Person extends AbstractBaseModel { }
@Entity
public class APerson extends Person { }
@Entity
public class BPerson extends Person { }
@Entity
public class Course extends AbstractBaseModel {
@ManyToOne
@JoinColumn(name ="person")
private Person person;
}
Above structure will give an exception:
Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on
Course references an unknown entity: Person
It says you cannot use Person in mappings because it is not a concrete Entity. How do I achieve such an inheritance scenario?
Simple, you change the @MappedSuperclass
annotation on Person
to @Entity
Use @MappedSuperclass
only where you explicitly do not want the class to be queryable, or part of a relation. @Entity
everywhere else.
A good heuristic to decide is to see whether your superclass is abstract
- if it is - use @MappedSuperclass
, like you did on the AbstractBaseModel
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With