Hibernate Table Per Concrete Class Spring Boot

In this post, we will see Hibernate Table Per Concrete Class Example Using Spring Boot and Oracle. We are going to use a maven, embedded tomcat, postman and oracle database. Here we will have rest endpoint which will be used to save data in the database.

Hibernate Table Per Concrete Class Spring Boot

Let’s see some point related to Hibernate Table Per Concrete Class Spring Boot Example.

  • In table per concrete class mapping, it will create a separate table for each entity.
  • @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) is used with parent entity.
  • Table created for parent entity will have only parent attribute/column whereas child entity will have both parents as well as child attribute/column.

 

Tables details after running this example.

Hibernate Table Per Concrete Class Spring Boot

Hibernate Table Per Concrete Class Spring Boot

Hibernate Table Per Concrete Class Spring Boot

 

Let’s see the Hibernate Table Per Concrete Class Spring Boot Example.

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 – tableperconcreteclasshibernatejpa, ArtifactId – tableperconcreteclasshibernatejpa and name – tableperconcreteclasshibernatejpa) 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

The query generated while one record.

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

Now we will prepare JSON data and will try save in database.

Sample request JSON data-

 

Response –

 

Let’s check the database we have after running this example.

Hibernate Table Per Concrete Class Spring Boot

 

That’s all about Hibernate Table Per Concrete Class Spring Boot Example.

You may like.

Association Mapping in Hibernate using Spring Boot and Oracle Example.

 

Spring Data JPA examples.

Inheritance Mapping docs.

Summary – We have seen Hibernate Table Per Concrete Class Spring Boot. A separate table for each entity.

we have seen Table Per Concrete Class Spring Boot And Oracle.

Top