Hibernate API supports Inheritance mapping in 3 ways
Table per class hierarchy
Table per subclass
Table per concrete class
Table per sub class
In this example we will see how to implement one Table per sub class . It means each class persist the data in its own separate table but a foreign key relationship exists between the subclass tables and super class table and common data will store in to the parent class
Let us Consider Vehicle is root class and Bus and Car are sub classes. Vehicle id will be as foreign key in Bus and Car table and common data is stored in VEHICLE table and subclass specific fields are stored in BUS and CAR tables.
Create a Java project and update Hibernate jars and Mysql driver jar in to build path. (Download Hibernate , MySql Driver)
2. Create a Vehicle class
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;
@Entity
@Table(name = "VEHICLE")
@Inheritance(strategy = InheritanceType.JOINED)
public class Vehicle {
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
@Column(name = "Vehicle_Id")
int VehicleId;
@Column(name = "Vehicle_Name")
String VehicleName;
public Vehicle() {
}
public Vehicle(String VehicleName) {
this.VehicleName = VehicleName;
}
public int getVehicleId() {
return VehicleId;
}
public void setVehicleId(int vehicleId) {
VehicleId = vehicleId;
}
public String getVehicleName() {
return VehicleName;
}
public void setVehicleName(String vehicleName) {
VehicleName = vehicleName;
}
}
3. Create a sub class (Car.java)
import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.Table;
@Entity
@Table(name="CAR")
public class Car extends Vehicle {
@Column(name = "Car_Model")
String carmodel;
public Car() {
}
public Car(String vehicleName, String carmodel) {
super(vehicleName);
this.carmodel = carmodel;
}
public String getCarmodel() {
return carmodel;
}
public void setCarmodel(String carmodel) {
this.carmodel = carmodel;
}
}
4.Create Bus entity (Bus.java)
import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.Table;
@Entity
@Table(name="BUS")
public class Bus extends Vehicle {
@Column(name = "Bus_Model")
String busmodel;
public Bus() {
}
public Bus(String vehicleName, String busmodel) {
super(vehicleName);
this.busmodel = busmodel;
}
public String getBusmodel() {
return busmodel;
}
public void setBusmodel(String busmodel) {
this.busmodel = busmodel;
}
}
5.Create Hibernate config file(hibernate.cfg.xml)
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="studentFactory">
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/hibernateschema
</property>
<property name="connection.username">
root
</property>
<property name="connection.password">
root
</property>
<property name="connection.pool_size">5</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping class="com.pretech.Vehicle"></mapping>
<mapping class="com.pretech.Bus"></mapping>
<mapping class="com.pretech.Car"></mapping>
</session-factory>
</hibernate-configuration>
6. Create a main program to Save Vehicles
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class VehicleMain {
private static SessionFactory sessionFactory;
public static void main(String[] args) {
try {
sessionFactory = new Configuration().configure("hibernate.cfg.xml")
.buildSessionFactory();
} catch (Exception e) {
System.out.println(e.getMessage());
}
Session session = sessionFactory.openSession();
Vehicle vehicle = new Vehicle("Four Wheeler");
Bus bus = new Bus("Bus", "Volvo");
Car car = new Car("Car", "Honda");
session.beginTransaction();
session.save(vehicle);
session.save(bus);
session.save(car);
session.getTransaction().commit();
System.out.println("Vehicle objects Saved");
if (session != null)
session.close();
}
}
7. Run it
Once you run the program we will see below output
Hibernate: insert into VEHICLE (Vehicle_Name, Vehicle_Id) values (?, ?)
Hibernate: insert into VEHICLE (Vehicle_Name, Vehicle_Id) values (?, ?)
Hibernate: insert into BUS (Bus_Model, Vehicle_Id) values (?, ?)
Hibernate: insert into VEHICLE (Vehicle_Name, Vehicle_Id) values (?, ?)
Hibernate: insert into CAR (Car_Model, Vehicle_Id) values (?, ?)
Vehicle objects Saved
Database Table output
Download Source Code
Hibernate Inheritance Table per sub class Example
No comments:
Post a Comment