Hibernate Inheritance – Table Per Concrete Class Hierarchy Example(XML Mapping & Annotation)

Table per Class Hierarchy strategy will be having a single table for both the Parent class and the Sub class separated by a discriminator column. In Table per Subclass Hierarchy, subclass table will be mapped to the Parent class table by primary key and foreign key relationship. In Table Per Concrete Class Hierarchy also individual table will be created for each class. The disadvantage of this approach is that all the columns of the Primary class table will be again replicated in the Subclass table.

Parent class Vehicle will be inherited by the subclass Bus, we will be having a separate table for holding both Vehicle and Bus data. “VEHICLE_ID” will act as the primary key for both the table and BUS will hold all the column of the VEHICLE table.

Vehicle

Table Per Concrete Class Hierarchy Example

Creating table

Create VEHICLE, BUS Table, simply Copy and Paste the following SQL query in the query editor to get the table created.

CREATE TABLE "VEHICLE" 
 ( "VEHICLE_ID" NUMBER(10,0) NOT NULL , 
 "ENGINE" VARCHAR2(20 BYTE) NULL , 
 "WHEELS" VARCHAR2(10 BYTE) NULL,
 PRIMARY KEY (VEHICLE_ID)
 );
 
 CREATE TABLE "BUS" 
 ( "VEHICLE_ID" NUMBER(10,0) NOT NULL, 
 "ENGINE" VARCHAR2(20 BYTE) NULL , 
 "WHEELS" VARCHAR2(10 BYTE) NULL,
 "BUS_TYPE" VARCHAR2(255 CHAR), 
 PRIMARY KEY (VEHICLE_ID)
 );

Folder Structure:

Table Per Concrete class Hierarchy Example

  1. Create a simple Maven Project “HibernateTutorial” and create a package for our source files com.javainterviewpointunder  src/main/java 
  2. Now add the following dependency in the POM.xml
     <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
       <modelVersion>4.0.0</modelVersion>
       <groupId>HibernateTutorial</groupId>
       <artifactId>HibernateTutorial</artifactId>
       <version>0.0.1-SNAPSHOT</version>
       <properties>
          <hibernate.version>4.3.11.Final</hibernate.version>
          <oracle.connector.version>11.2.0</oracle.connector.version>
       </properties>
    
       <dependencies>
         <!-- Hibernate -->
         <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
         </dependency>
    
         <!-- Oracle -->
         <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc14</artifactId>
            <version>${oracle.connector.version}</version>
         </dependency>
       </dependencies>
       <build>
         <sourceDirectory>src</sourceDirectory>
         <plugins>
           <plugin>
             <artifactId>maven-compiler-plugin</artifactId>
             <version>3.3</version>
             <configuration>
             <source>1.7</source>
             <target>1.7</target>
             </configuration>
           </plugin>
         </plugins>
       </build>
     </project>
  3. Create the Java classes Vehicle.java, Bus.java   and TablePerConcreteClassHierarchy.java under  com.javainterviewpoint folder.
  4. Place the vehicle.hbm.xml and hibernate.cfg.xml under the src/main/resources  directory

Table Per Concrete class Hierarchy Example using XML Mapping

Vehicle.java

Create a new Java file Vehicle.java under the package com.javainterviewpoint and add the following code

package com.javainterviewpoint;

public class Vehicle
{
    private int vehicleId;
    private String engine;
    private String wheels;
    
    public Vehicle()
    {
        super();
    }
    public Vehicle(int vehicleId, String engine, String wheels)
    {
        super();
        this.vehicleId = vehicleId;
        this.engine = engine;
        this.wheels = wheels;
    }
    
    public int getVehicleId()
    {
        return vehicleId;
    }
    public void setVehicleId(int vehicleId)
    {
        this.vehicleId = vehicleId;
    }
    public String getEngine()
    {
        return engine;
    }
    public void setEngine(String engine)
    {
        this.engine = engine;
    }
    public String getWheels()
    {
        return wheels;
    }
    public void setWheels(String wheels)
    {
        this.wheels = wheels;
    }
    @Override
    public String toString()
    {
        return "Vehicle [vehicleId=" + vehicleId + ", engine=" + engine + ", wheels=" + wheels + "]";
    }
}

Our Vehicle class is a simple POJO class consisting of the getters and setters for the Vehicle class properties (vehicleId, engine,wheels).

Bus.java

Create a new Java file Bus.java under the package com.javainterviewpoint and add the following code

package com.javainterviewpoint;

public class Bus extends Vehicle
{
    private String busType;

    public Bus()
    {
        super();
    }
    public Bus(String busType)
    {
        super();
        this.busType = busType;
    }

    public String getBusType()
    {
        return busType;
    }

    public void setBusType(String busType)
    {
        this.busType = busType;
    }
}

vehicle.hbm.xml

Place the vehicle.hbm.xml file under the src/main/resources folder

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

  <hibernate-mapping package="com.javainterviewpoint">

   <class name="Vehicle" table="VEHICLE">
     <id name="vehicleId" column="VEHICLE_ID">
       <generator class="native" />
     </id>
     <property name="engine" column="ENGINE" />
     <property name="wheels" column="WHEELS" />
    </class>

    <class name="Bus" table="BUS">
      <id name="vehicleId" column="VEHICLE_ID">
        <generator class="native" />
      </id>
      <property name="engine" column="ENGINE" />
      <property name="wheels" column="WHEELS" />
      <property name="busType" column="BUS_TYPE" />
    </class>
</hibernate-mapping>
  • The “vehicle.hbm.xml” tells hibernate to map “Vehicle.class” with the “VEHICLE” table in the database.
  • Next tag is the <id> tag, this tag tells which column needs to be marked as primary key in the database table, here our id property of the Vehicle class is the primary key. We have selected the generator as native, it takes the sequence in Oracle if no sequence name is provided then HIBERNATE_SEQUENCE” will be used
  • The property engine, wheels are mapped with ENGINE, WHEELS column in the table respectively.
  • We have marked Bus also using the <class> tag here and we have defined all the property of the Vehicle class as well.

hibernate.cfg.xml

Place the hibernate.cfg.xml file also under the src/main/resources folder

 <?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

 <session-factory>

 <!-- Database connection settings -->
 <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
 <property name="hibernate.connection.username">root</property>
 <property name="hibernate.connection.password">root</property>
 <property name="hibernate.connection.url">jdbc:oracle:thin:@mydb:40051:dev</property>

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

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

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

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

 <!-- Mapping resource file -->
 <mapping resource="vehicle.hbm.xml" />
</session-factory> 
</hibernate-configuration>
  • First and foremost property is for specifying the JDBC Driver class, in my case it OracleDriver
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
  • Give the connection URL for connecting the database and provide username and password for connecting the above database
<property name="hibernate.connection.url">jdbc:oracle:thin:@mydb:40051:dev</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
  • Specify the connection pool size, this property limits the number of connections in the Hibernate connection pool.
<property name="connection.pool_size">1</property>
  • Dialect Property makes the Hibernate generate the SQL for the corresponding database which is being used. In this example we are using Oracle database hence Oracle query will be generated. If you are using MySQL database then you need to change the dialect accordingly.
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
  • The show_sql property will print the executed sql in the console when set to true.
<property name="show_sql">true</property>
  • If the property “hibernate.hbm2ddl.auto” is set to “create”  This will drop and recreate the database schema on every execution. If it is set to “update” then the database schema will be updated every time rather than dropping and recreating.
<property name="hibernate.hbm2ddl.auto">update</property>
  • Under the Mapping resource tag, we need to specify all the mapping file for which we need the table to be created or updated.
<mapping resource="vehicle.hbm.xml" />

TablePerConcreteClassHierarchy.java

package com.javainterviewpoint;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class TablePerConcreteClassHierarchy
{
    public static void main(String args[])
    {
        //Reading the hibernate configuration file
        Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
        StandardServiceRegistryBuilder regBuilber = new StandardServiceRegistryBuilder();
        regBuilber.applySettings(configuration.getProperties());
        ServiceRegistry serviceRegistry = regBuilber.build();
        
        //Create SessionFacctory
        SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        
        //Create Session from SessionFactory
        Session session = sessionFactory.openSession();
        
        //Begin the transaction
        session.beginTransaction();
        
        Vehicle vehicle = new Vehicle();
        vehicle.setEngine("1300CC");
        vehicle.setWheels("8");
        session.save(vehicle);
        
        Bus bus = new Bus();
        
        bus.setBusType("Volvo");
        bus.setWheels("6");
        bus.setEngine("1000CC");
        session.save(bus);
        
        //Commit the changes
        session.getTransaction().commit();
        //Close the session
        session.close();
    }
}
  • Create the Configuration object and read the configuration file using the configure() method.
Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
  • Get the SessionFactory object through the buildSessionFactory() method of the configuration object.
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
  • openSession() method opens up the new session and begin a new transaction
Session session = sessionFactory.openSession();
session.beginTransaction();
  • Create Vehicle object and set values to its properties
   Vehicle vehicle = new Vehicle();
   vehicle.setEngine("1300CC");
   vehicle.setWheels("8");
  • Create Bus object and set value to it properties
    Bus bus = new Bus();
    bus.setEngine("1500CC");;
    bus.setWheels("6");
    bus.setBusType("Volvo");
  • save() method of the session object will persist the Vehicle and Bus objects
session.save(vehicle);
session.save(bus);
  • Finally get the transaction and commit the changes and close the session.
session.getTransaction().commit();
session.close();

Console:

INFO: HHH000261: Table found: BUS
Feb 15, 2017 11:40:08 AM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000037: Columns: [engine, wheels, bus_type, vehicle_id]
Feb 15, 2017 11:40:08 AM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000108: Foreign keys: []
Feb 15, 2017 11:40:08 AM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000126: Indexes: [sys_c0015156]
Feb 15, 2017 11:40:11 AM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000261: Table found: VEHICLE
Feb 15, 2017 11:40:11 AM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000037: Columns: [engine, wheels, vehicle_id]
Feb 15, 2017 11:40:11 AM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000108: Foreign keys: []
Feb 15, 2017 11:40:11 AM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000126: Indexes: [sys_c0015154]
Feb 15, 2017 11:40:11 AM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000232: Schema update complete
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into VEHICLE (ENGINE, WHEELS, VEHICLE_ID) values (?, ?, ?)
Hibernate: insert into BUS (ENGINE, WHEELS, BUS_TYPE, VEHICLE_ID) values (?, ?, ?, ?)

                  Table Per Concrete class Hierarchy    Table Per Concrete class Hierarchy 1

Table Per Concrete class Hierarchy Example using Annotations

We will be adding JPA Annotation to our Vehicle and Bus classes.

Vehicle.java

package com.javainterviewpoint;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;

@Entity
@Table(name="VEHICLE")
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class Vehicle
{
    @Id
    @GeneratedValue
    @Column(name="VEHICLE_ID")
    private int vehicleId;
    
    @Column(name="ENGINE")
    private String engine;
    
    @Column(name="WHEELS")
    private String wheels;
    
    public Vehicle()
    {
        super();
    }
    public Vehicle(int vehicleId, String engine, String wheels)
    {
        super();
        this.vehicleId = vehicleId;
        this.engine = engine;
        this.wheels = wheels;
    }
    
    public int getVehicleId()
    {
        return vehicleId;
    }
    public void setVehicleId(int vehicleId)
    {
        this.vehicleId = vehicleId;
    }
    public String getEngine()
    {
        return engine;
    }
    public void setEngine(String engine)
    {
        this.engine = engine;
    }
    public String getWheels()
    {
        return wheels;
    }
    public void setWheels(String wheels)
    {
        this.wheels = wheels;
    }
    @Override
    public String toString()
    {
        return "Vehicle [vehicleId=" + vehicleId + ", engine=" + engine + ", wheels=" + wheels + "]";
    }
}

Bus.java

package com.javainterviewpoint;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

@Entity
@Table(name = "BUS")
@AttributeOverrides({
    @AttributeOverride(name="engine", [email protected](name="ENGINE")),
    @AttributeOverride(name="wheels", [email protected](name="WHEELS"))
})
public class Bus extends Vehicle
{
    @Column(name="BUS_TYPE")
    private String busType;

    public Bus()
    {
        super();
    }
    public Bus(String busType)
    {
        super();
        this.busType = busType;
    }

    public String getBusType()
    {
        return busType;
    }

    public void setBusType(String busType)
    {
        this.busType = busType;
    }
}

We have used the below JPA Annotations in our Vehicle and Bus class

  1. @Entity – This annotation will mark our Employee class as an Entity Bean.
  2. @Table@Table annotation will map our class to the corresponding database table. You can also specify other attributes such as indexes, catalog, schema, uniqueConstraints. The @Table annotation is an optional annotation if this annotation is not provided then the class name will be used as the table name.
  3. @Id –  The @Id annotation marks the particular field as the primary key of the Entity.
  4. @GeneratedValue – This annotation is used to specify how the primary key should be generated. Here SEQUENCE Strategy will be used as this the default strategy for Oracle
  5. @Column – This annotation maps the corresponding fields to their respective columns in the database table.
  6. @Inheritance – This annotation defines the inheritance strategy which will be used and it should be defined in the root entity class (Vehicle). Here we have used TABLE_PER_CLASS Strategy.
  7. @AttributeOverrides – This annotation overrides the multiple properties of the parent class
  8. @AttributeOverride – This annotation overrides the individual property of the parent class. We have VehicleId, engine, wheel property in the parent class (Vehicle), in order to override it in the subclass we will be using this annotation.

Leave a Reply

Your email address will not be published. Required fields are marked *