DEV Community

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

Posted on

1

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.

API Trace View

Struggling with slow API calls? đź‘€

Dan Mindru walks through how he used Sentry's new Trace View feature to shave off 22.3 seconds from an API call.

Get a practical walkthrough of how to identify bottlenecks, split tasks into multiple parallel tasks, identify slow AI model calls, and more.

Read more →

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

đź‘‹ Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay