DEV Community

Cover image for TIL: @MappedSuperclass vs. @Inheritance (JPA)
Bruno Panassi
Bruno Panassi

Posted on

TIL: @MappedSuperclass vs. @Inheritance (JPA)

Today i needed to learn the difference between MappedSuperclass and Inheritance especially for use in my personal project.

What do i needed in my project

There was two tables, Person and Role. The Person table it's a super table, so there was more tables that inherits properties and methods from it, does not have the need for a table Person in the database, so Person does not use the Entity annotation.

But, i need to use the OneToMany and ManyToOne annotation between Person and Role tables, but how Person does not use Entity, the OneToMany annotation was not possible in Role (Yes, i also learned this).

So, the difference between the annotations and my use for each it.

I searched in the StackOverflow and haved founded these links:
JPA: Implementing Model Hierarchy - @MappedSuperclass vs. @Inheritance

@MappedSuperclass and @OneToMany

What do i have learned from that:

  • MappedSuperclass: Will only be used to inherits props and methods and will not create a table in the database for it.

  • Inheritance: With the use of the prop (strategy = InheritanceType.TABLE_PER_CLASS) and the Entity annotation, will inherits the props and methods, and also, create a table for it in the database.

if there is some misunderstood in my text, let me know so i can change it.

Thanks for reading and i hope i helped.

Top comments (0)