Joined inheritance strategy.
Here to pull all the records you need to write join query.
Eg.
Select * from Vehicle join fourwheeler on vehicle.vehicleid
= fourwheeler.vehicleid
HibernateTest.java
package org.yash.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.yash.dto.FourWheeler;
import org.yash.dto.TwoWheeler;
import org.yash.dto.Vehicle;
public class HibernateTest {
public static void main(String
args[]) {
Vehicle vehicle =
new Vehicle();
vehicle.setVehicleName("Car");
TwoWheeler bike =
new TwoWheeler();
bike.setVehicleName("Bike");
bike.setSteeringHandle("Bike
Steering Handle");
FourWheeler car =
new FourWheeler();
car.setVehicleName("Porsche");
car.setSteeringWheel("Porsche
Steering Wheel");
SessionFactory
sessionFactory = new Configuration().configure()
.buildSessionFactory();
Session session =
sessionFactory.openSession();
/* It is used
to save all the objects and to define single unit of work */
session.beginTransaction();
session.save(vehicle);
session.save(bike);
session.save(car);
session.getTransaction().commit();
session.close();
}
}
Vehicle.java
package org.yash.dto;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public class Vehicle {
@Id @GeneratedValue
private int vehicleId;
private String vehicleName;
public int getVehicleId()
{
return vehicleId;
}
public void setVehicleId(int vehicleId) {
this.vehicleId = vehicleId;
}
public String
getVehicleName() {
return vehicleName;
}
public void
setVehicleName(String vehicleName) {
this.vehicleName = vehicleName;
}
}
TwoWheeler.java
package org.yash.dto;
import javax.persistence.Entity;
@Entity
public class TwoWheeler extends Vehicle {
private String SteeringHandle;
public void
setSteeringHandle(String steeringHandle) {
SteeringHandle =
steeringHandle;
}
public String
getSteeringHandle() {
return SteeringHandle;
}
}
FourWheeler.java
package org.yash.dto;
import javax.persistence.Entity;
@Entity
public class FourWheeler extends Vehicle {
private String steeringWheel;
public void setSteeringWheel(String
steeringWheel) {
this.steeringWheel =
steeringWheel;
}
public String
getSteeringWheel() {
return steeringWheel;
}
}
Happy to found this blog. Good Post!. It was so good to read and useful to improve my knowledge as updated one, keep blogging. Hibernate Training in Electronic City
ReplyDeleteJava Training in Electronic City