When generating a database using JPA, one of the most important things we have to do is mapping inheritances. In this post, you are going to learn some basic things that we have to know when mapping inheritance.

Image for post

Inheritance

For the ScholarX project, I had to add JPA annotations to the spring-boot project in order to generate the MySQL database. The models had already been created for the entities. To initiate the task, I was given a class diagram. Otherwise, it might be so hard to find data types, relationships, etc. Click here if you wanna find out what a class diagram is.


Okay then… back to the topic. JPA has several inheritance strategies.

  • MappedSuperclass
  • Single Table
  • Joined Table
  • Table-Per-Class

If you have checked our class diagram you may notice that we have a base modal called “BaseScholarxModel” for all entities. And there’s another parent called “EnrolledUser” extended by the base modal. We don’t need tables for these two models. Therefore I had to make use of those different strategies mentioned above.

Image for post

A section of the class diagram

#programming #hibernate #spring #jpa #object-oriented

Inheritance Mapping with JPA/Hibernate
1.20 GEEK