75

I am using Play Framework 1.2.4 with PostgreSQL and JPA. I would like to have a Model hierarchy and see that there are some alternatives to doing this.

I have a base class (which is abstract) and two concrete classes extending this base class. I don't want to persist this base class while I want to have concrete classes. In the base class, I have another Model classes as properties, in other words, I have @ManyToOne relationships in my base class.

My question is what is the best way of implementing this? Using @MappedSuperclass or @Inheritance with TABLE_PER_CLASS strategy? I am a bit confused as they seem virtually equivalent.

I also have some concerns about querying and performance issues that I might face in future.

1
  • 3
    I've changed @Inheritence to @Inheritance as I assumed it was a typo.
    – Cœur
    Dec 15, 2017 at 8:19

2 Answers 2

167

MappedSuperClass must be used to inherit properties, associations, and methods.

Entity inheritance must be used when you have an entity, and several sub-entities.

You can tell if you need one or the other by answering this questions: is there some other entity in the model which could have an association with the base class?

If yes, then the base class is in fact an entity, and you should use entity inheritance. If no, then the base class is in fact a class that contains attributes and methods that are common to several unrelated entities, and you should use a mapped superclass.

For example:

  • You can have several kinds of messages: SMS messages, email messages, or phone messages. And a person has a list of messages. You can also have a reminder linked to a message, regardless of the kind of message. In this case, Message is clearly an entity, and entity inheritance must be used.
  • All your domain objects could have a creation date, modification date and ID, and you could thus make them inherit from a base AbstractDomainObject class. But no entity will ever have an association to an AbstractDomainObject. It will always be an association to a more specific entity: Customer, Company, whatever. In this case, it makes sense to use a MappedSuperClass.
4
  • Many thanks for your answer. I am now clear on that if I need to persist or use base class as an entity I should use entity inheritance. I wonder if there is a problem with performance or restriction on queries especially using inherited object relations?
    – huzeyfe
    Mar 12, 2012 at 15:32
  • It all depends on which kind of inheritance you choose, and which inheritance strategy.
    – JB Nizet
    Mar 12, 2012 at 15:34
  • In my case (as I don't want to persist base class) MappedSuperClass seem more suitable. So in terms of performance and restrictions do I need to be aware of some certain issues?
    – huzeyfe
    Mar 12, 2012 at 15:38
  • 4
    No. It will work exactly as if the mapped superclass attributes were in the entity directly.
    – JB Nizet
    Mar 12, 2012 at 16:19
40

@MappedSupperclass is different than the @Inheritance annotation.

@MappedSuperclass tells the JPA provider to include the base class persistent properties as if they were declared by the child class extending the superclass annotated with @MappedSuperclass.

However, the inheritance is only visible in the OOP world, since, from a database perspective, there's no indication of the base class. Only the child class entity will have an associated mapped table.

The @Inheritance annotation is meant to materialize the OOP inheritance model in the database table structure. More, you can query a base class annotated with @Inheritance but you can't do that for a base class annotated with @MappedSuperclass.

Now, the reason why you'd want to use the @Inheritance JPA annotation is to implement behavior-driven patterns like the Strategy Pattern.

On the other hand, @MappedSuperclass is just a way to reuse both basic properties, associations, and even the entity @Id using a common base class. Nevertheless, you can achieve almost the same goal using an @Embeddable type. The only major difference is that you can't reuse an @Id definition with @Embeddable, but you can do it with @MappedSuperclass.

8
  • 4
    "The only major difference is that you can't reuse an Id definition with Embeddable, but you can do it with MappedSuperclass" thank you, I have been trying to find this for almost an hour. Mar 19, 2018 at 14:57
  • The great thing about '@Inheritance' is, that we can use polymorphic queries. Setting '@Inheritance' to TABLE_PER_CLASS, what's a difference between that and '@MappedSuperclass'?
    – Serafins
    Dec 4, 2020 at 12:47
  • 1
    TABLE_PER_CLASS is very inefficient due to the UNION ALL queries. Better use SINGLE_TABLE or JOINED, as explained in my High-Performance Java Persistence book. Dec 4, 2020 at 13:25
  • THANK YOU, makes it much clearer Feb 9, 2022 at 22:09
  • @JulioFaerman I'm glad I could help Feb 10, 2022 at 6:48

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.