Hibernate Table Per Subclass Inheritance Spring Boot

In this post, we will see Hibernate Table Per Subclass Inheritance Example Using Spring Boot. We are going to use a maven, embedded tomcat, postman and oracle database.

Hibernate Table Per Subclass Inheritance

 

Let’s see some points related to Hibernate Table Per Subclass Inheritance.

  1. In Table Per Subclass Mapping, subclass mapped tables are related to parent class mapped table with the primary and foreign key relationship.
  2. We need to mention @Inheritance(strategy=InheritanceType.JOINED) with parent class(Insurance.java) and @PrimaryKeyJoinColumn(name=”id”) with child classes(VehicleInsurance.java and HealthInsurance.java).
  3. There will be a separate table for all entity(i.e insurance, health_insurance and vehicle_insurance).

Let’s see the sample table we will have after running this example and inserting one record.

 

Hibernate Table Per Subclass Inheritance Spring Boot

Spring Boot Table Per Subclass

 

Rest endpoint which will be used to save data in the database.

Let’s see the complete example of Hibernate Table Per Subclass Inheritance Spring Boot.

Create a maven project using eclipse and modify pom.xml.

Note – We will not create the table, let’s hibernate do this job. In application.properties file we will keep spring.jpa.hibernate.ddl-auto =create

Open eclipse and create maven project, Don’t forget to check ‘Create a simple project (skip)’ click on next.  Fill all details(GroupId – tablepersubclasshibernatejpa, ArtifactId – tablepersubclasshibernatejpa and name – tablepersubclasshibernatejpa) and click on finish. Keep packaging as the jar.

Note – In pom.xml we have defined javac.exe path in configuration tag. You need to change accordingly i.e where you have installed JDK. If you see any error for oracle dependency then follow these steps.

 

Define entity classes i.e Insurance.java, HealthInsurance.java and VehicleInsurance.java.

Insurance.java

HealthInsurance.java

 

VehicleInsurance.java.

Define the repository interface extending CrudRepository.

 

Define service interface i.e InsuranceService.java

 

Define service implementation class.

Note – See here more about @Component, @Controller, @Service and @Repository annotations here.

Define the controller class or endpoint.

Note – See more details about @Controller and RestController here.

Define the JpaConfig.java

Note – See more details about @Configuration annotations here.

Define the SpringMain.java.

Note – See more details about @ComponentScan here.

application.properties

 

 

Generated query.

Hibernate: insert into insurance (company_name, id) values (?, ?)
Hibernate: insert into insurance (company_name, id) values (?, ?)
Hibernate: insert into health_insurance (age, person_name, id) values (?, ?, ?)
Hibernate: insert into insurance (company_name, id) values (?, ?)
Hibernate: insert into vehicle_insurance (number_of_wheels, vehicle_model, id) values (?, ?, ?)

 

That’s all about Hibernate Table Per Subclass Inheritance Example Using Spring Boot.

You may like.

Association Mapping Examples Using Spring Boot and Oracle.

Spring Data JPA examples.

 

Summary – We have seen Hibernate Table Per Subclass Inheritance Example Using Spring Boot. A separate table created for all concrete subclasses.

Top