gudii9
asked on
hibernate table per class strategy
Hi,
I am running below example on hibernate table per class strategy
hibernate cfg xml looks like
client class looks like
vehicle class looks like
FourWheeler looks like
I do not see dtype any more in the inserted table. When do we use table per class strategy and what are its advantages.
Please advise. Any ideas, resources, sample code highly appreciated. thanks in advance
FourWheeler.jpg
TwoWheeler.jpg
vehicle.jpg
I am running below example on hibernate table per class strategy
hibernate cfg xml looks like
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/hibernatedb</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">root</property>
<property name="hibersnate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<mapping class="org.gp.gpr.dto.UserDetails"/>
<mapping class="org.gp.gpr.dto.Vehicle"/>
<mapping class="org.gp.gpr.dto.TwoWheeler"/>
<mapping class="org.gp.gpr.dto.FourWheeler"/>
</session-factory>
</hibernate-configuration>
client class looks like
package org.gp.gpr.hibernate;
import java.util.Date;
import org.gp.gpr.dto.Address;
import org.gp.gpr.dto.FourWheeler;
import org.gp.gpr.dto.TwoWheeler;
import org.gp.gpr.dto.UserDetails;
import org.gp.gpr.dto.Vehicle;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateTestInheritance {
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("toyota");
car.setSteeringWheel("toyota steering wheeling");
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session=sessionFactory.openSession();
session.beginTransaction();
session.save(vehicle);
session.save(bike);
session.save(car);
session.getTransaction().commit();
session.close();
}
}
vehicle class looks like
package org.gp.gpr.dto;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
//@DiscriminatorColumn(
// name="VEHICLE_TYPE",
// discriminatorType=DiscriminatorType.STRING
// )
public class Vehicle {
@Id@GeneratedValue
private int vehicleId;
private String vehicleName;
//@ManyToOne
//@JoinColumn(name="USER_ID")
//@ManyToMany(mappedBy="vehicle")
//private Collection<UserDetails> userList=new ArrayList();;
//@NotFound(action=NotFoundAction.IGNORE)
//private UserDetails user;
//public Collection<UserDetails> getUserList() {
// return userList;
//}
//public void setUserList(Collection<UserDetails> userList) {
// this.userList = userList;
//}
//public UserDetails getUser() {
// return user;
//}
//public void setUser(UserDetails user) {
// this.user = user;
//}
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 looks likepackage org.gp.gpr.dto;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
@Entity
//@DiscriminatorValue("Bikee")
public class TwoWheeler extends Vehicle {
private String SteeringHandle;
public String getSteeringHandle() {
return SteeringHandle;
}
public void setSteeringHandle(String steeringHandle) {
SteeringHandle = steeringHandle;
}
}
FourWheeler looks like
package org.gp.gpr.dto;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
@Entity
//@DiscriminatorValue("carr")
public class FourWheeler extends Vehicle {
private String SteeringWheel;
public String getSteeringWheel() {
return SteeringWheel;
}
public void setSteeringWheel(String steeringWheel) {
SteeringWheel = steeringWheel;
}
}
I do not see dtype any more in the inserted table. When do we use table per class strategy and what are its advantages.
Please advise. Any ideas, resources, sample code highly appreciated. thanks in advance
FourWheeler.jpg
TwoWheeler.jpg
vehicle.jpg
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Initial SessionFactory creation failed.java.lang.ClassForm
Exception in thread “main” java.lang.ExceptionInIniti
at net.viralpatel.hibernate.H
at net.viralpatel.hibernate.H
at net.viralpatel.hibernate.M
Caused by: java.lang.ClassFormatError
at java.lang.ClassLoader.defi