Hibernate inheritance:table per subclass

This is 7 of 8 parts of tutorial series

Tutorial Content:

    Introduction to hibernate framework Hibernate hello world example in eclipse Hibernate interview questions Hibernate one to one mapping example Hibernate one to many mapping example Hibernate many to many mapping example Hibernate inheritance:Table per class hierarchy Hibernate inheritance:table per subclass Hibernate inheritance:Table per concrete class Difference between openSession and getCurrentSession
In this tutorial,we will see how to implement inheritance in hibernate.There are 3 ways in which you can implement inheritance in hibernate.In this post,we will see one of them i.e.one table per concrete class.

Inheritance in hibernate:

Java is object oriented language and inheritance is one of main functionalities of java.Relation model can implement "is a" and "has a" relationship but hibernate provides us way to implement class hierarchy in a different ways.

Table per concrete class:

Lets say we have following class hierarchy.We have shape class as base class and Rectangle and Circle inherit from Shape class.

 In  table per concrete class ,One table will be created for each concrete class..So there table will be created SHAPE,RECTANGLE and CIRCLE and subclass repeats property of parent class.

As per our class diagram,we will create three classes-Shape.java,Rectangle.java and Circle.java


This is our root class of entity class hierarchy.
Create Shape.java in src->org.arpit.javapostsforlearning.

package org.arpit.javapostsforlearning;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;

public class Shape {

 @GeneratedValue(strategy = GenerationType.TABLE)
 int shapeId;
 String shapeName;
 public Shape()
 public Shape(String shapeName)
 //getters and setters

Shape is our root class so some annotations needs to be used with root class for implementing inheritance.


For implementing inheritance in hiberante,@Inheritance annotation is used.It defines inheritance strategy to be implement for entity class hierarchy.For one table per class hierarhcy,we have used Table_Per_Class as inheritance strategy.This annotation is defined at root level or sub hierarchy level where different strategy is to be applied.


This is our child class.
Create Rectangle.java in src->org.arpit.javapostsforlearning.

package org.arpit.javapostsforlearning;import javax.persistence.Column;
import javax.persistence.Column;
import javax.persistence.Entity;

public class Rectangle extends Shape{

 int length;
 int breadth;
 // getters and setters
 public Rectangle()
 public Rectangle(String shapeName,int length,int breadth)
 // getters and setters


This is our second child class.
Create Circle .java in src->org.arpit.javapostsforlearning.
package org.arpit.javapostsforlearning;import javax.persistence.Column;import javax.persistence.Column;
import javax.persistence.Entity;

public class Circle extends Shape{

 int radius;
 public Circle()
 public Circle(String shapeName,int radius)
 // getters and setters


Create a file named "hibernate.cfg.xml" in src folder.
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"



        <!-- Database connection settings -->
        <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
        <property name="connection.url">jdbc:sqlserver://localhost:1433;database=UserInfo</property>
        <property name="connection.username">sa</property>
        <property name="connection.password"></property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.SQLServer2005Dialect</property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">create</property>

  <mapping class="org.arpit.javapostsforlearning.Shape"></mapping>
  <mapping class="org.arpit.javapostsforlearning.Rectangle"></mapping>
  <mapping class="org.arpit.javapostsforlearning.Circle"></mapping>



5.Main Class:

package org.arpit.javapostsforlearning;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class HibernateMain {

 public static void main(String[] args) {
  Shape shape=new Shape("Sqaure");
  Rectangle rectangle=new Rectangle("Rectangle", 10, 20);  
  Circle circle=new Circle("Circle", 4);
  Configuration configuration=new Configuration();
  ServiceRegistry sr= new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
  SessionFactory sf=configuration.buildSessionFactory(sr);
  Session ss=sf.openSession();

Project Structure:


6.Run it:

When you run it,you will get following output.

Hibernate: create table Circle (Shape_Id int not null, Shape_Name varchar(255), Circle_Radius int, primary key (Shape_Id))
Hibernate: create table Rectangle (Shape_Id int not null, Shape_Name varchar(255), Rectangle_Breadth int, Rectangle_Length int, primary key (Shape_Id))
Hibernate: create table SHAPE (Shape_Id int not null, Shape_Name varchar(255), primary key (Shape_Id))
Hibernate: create table hibernate_sequences ( sequence_name varchar(255),  sequence_next_hi_value int ) 
Feb 05, 2013 11:17:13 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000230: Schema export complete
Hibernate: select sequence_next_hi_value from hibernate_sequences with (updlock, rowlock ) where sequence_name = 'SHAPE'
Hibernate: insert into hibernate_sequences(sequence_name, sequence_next_hi_value) values('SHAPE', ?)
Hibernate: update hibernate_sequences set sequence_next_hi_value = ? where sequence_next_hi_value = ? and sequence_name = 'SHAPE'
Hibernate: insert into SHAPE (Shape_Name, Shape_Id) values (?, ?)
Hibernate: insert into Rectangle (Shape_Name, Rectangle_Breadth, Rectangle_Length, Shape_Id) values (?, ?, ?, ?)
Hibernate: insert into Circle (Shape_Name, Circle_Radius, Shape_Id) values (?, ?, ?)

7.SQL output:

SHAPE table in database.

Rectangle table in database

Circle table in database

Written by Arpit:

If you have read the post and liked it. Please connect with me on Facebook | Twitter | Google Plus


Java tutorial for beginners Copyright © 2012