Search code examples
javahibernatejpajpa-2.1hibernate-5.x

Why is the MappedSuperClass annotation no longer valid in combination with @Inheritance when migrating to Hibernate 5.4.x?


I'm migrating several code bases to use Hibernate 5.4.x instead of Hibernate 5.2.x.

For an abstract base class I use

@MappedSuperclass
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class AbstractPersistentJPAObject extends AbstractPersistentObject {
    // some properties
}

However, the ORM complains about this with the error message

An entity cannot be annotated with both @Inheritance and @MappedSuperclass

This was not a problem with Hibernate <= 5.2.x and now I wonder why this should not be allowed any more.

Questions

  1. Is this a bug or a feature? If a feature: What the rationale behind this change?
  2. What can be done to circumvent the situation?
  3. If "circumvent" is not a valid idea: How should the above code fragment be changed to migrate it towards Hibernate >= 5.4.x correctly.

Any solid answers welcome.


Solution

  • My answers are given below:

    1. The mapping is incorrect according to the JPA 2.2 specification, @MappedSuperclass and @Inheritance cannot be used together. It seems, the mapping above was tolerated in earlier Hibernate releases. However, this support seems to be removed in Hibernate 5.4.x.

    2. To solve the issue in this specific scenario described above, you can replace @MappedSuperclass with @Entity and it should work fine.

    There is also a similar question in the Hibernate forum.

    You can also check HHH-13217, in which Gail Badner & Vlad Mihalcea (both Hibernate Developers) are discussing about this issue.

    In the next upcoming release 5.4.2, @Inheritence will be ignored, if it is used alongside @MappedSuperclass (see the related PR on Github).