Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate Inheritance - Referencing an entity annotated with @MappedSuperclass

@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?

like image 698
user706071 Avatar asked Mar 19 '13 15:03

user706071


1 Answers

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

like image 167
kostja Avatar answered Oct 12 '22 23:10

kostja