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

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

5 Comments

In that case, hibernate creates only Person class. It didn't create APerson or BPerson. Person entity merged APerson and BPerson.
@user706071 - I am not sure what you mean. Hibernate does not generate classes. How are APerson and BPerson different from Person? Can it be that they have no additional attributes and the differences are only behavioral?
APerson and BPerson classes have their own attributes other than the common ones (Person). Anyway, thanks for reply. I will use the approach you suggest. Hibernate created a table named Person merging all attributes of APerson and BPerson, and added a DTYPE attribute to distinguish subclass type. Since my APerson and BPerson are not very different, this kind of inheritance is applicable to me.
If they have, hibernate should be able to map them. Perhaps it is an issue with your Inheritance settings - all entities deriving from AbstractBseModel would go into a single table if you do not annotate it with a different inheritance strategy (SINGLE_TABLE is default). You could try JOINED to be sure that the additional tables are actually generated.
I have a similar question. Are you willing to help me with it? Here is the link: stackoverflow.com/questions/25252541/…
0
@Entity
@Table(name="PERSON")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="discriminator",discriminatorType=DiscriminatorType.STRING)
public absract 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", **insertable = false, updatable = false** )
  private Person person;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.