Hibernate Single Table Inheritance using Spring Boot

In this post, we will see Hibernate Single Table Inheritance using Spring Boot. 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 Single Table Inheritance using Spring Boot

 

Let’s see some point related to Hibernate Single Table Inheritance.

  • In Single Table Inheritance for all classes/entities which are in an inheritance relationship, there will be the only one table. 
  • @Inheritance(strategy=InheritanceType.SINGLE_TABLE) annotation is used with parent class. 
  • Apart from the above annotation @DiscriminatorColumn and @DiscriminatorValue used, for example we are going to use these annotations as (@DiscriminatorColumn(name=”type”,discriminatorType=DiscriminatorType.STRING) and @DiscriminatorValue(value=”INS”)).
  • @DiscriminatorValue needs to use with each entity which used to distinguish one entity from another.

Let’s see the sample table we will have after running this example.

hibernate single table inheritance

The entity with no fields will have null values.

Let’s see complete Hibernate Single Table Inheritance using Spring Boot and Oracle example from scratch.

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 – tableperclasshibernatejpa , ArtifactId – tableperclasshibernatejpa  and name – tableperclasshibernatejpa ) and click on finish. Keep packaging as the jar.

Modify pom.xml

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.

hibernate single table inheritance

Define entity class 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

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

Sample request JSON data-

 

Response – 

hibernate single table inheritance

 

This is all about Hibernate Single Table Inheritance using Spring Boot.

You may like.

Association Mapping in Hibernate using Spring Boot and Oracle Example.

Spring Data JPA examples.

 

Summary – We have seen Hibernate Single Table Inheritance using Spring Boot and Oracle. A single table created for all concrete subclasses. 

Top