Link to home
Create AccountLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

hibernate table per class strategy

Hi,

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>

Open in new window


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();

	
}
}

Open in new window


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;
}

}

Open in new window

TwoWheeler looks like
package 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;
}
}

Open in new window


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;
}
}

Open in new window


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
Avatar of Sathish David  Kumar N
Sathish David Kumar N
Flag of India image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of gudii9

ASKER

i tried that example getting below error

Initial SessionFactory creation failed.java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/validation/Validation
Exception in thread “main” java.lang.ExceptionInInitializerError
at net.viralpatel.hibernate.HibernateUtil.buildSessionFactory(HibernateUtil.java:18)
at net.viralpatel.hibernate.HibernateUtil.(HibernateUtil.java:8)
at net.viralpatel.hibernate.Main.main(Main.java:12)
Caused by: java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/validation/Validation
at java.lang.ClassLoader.defineClass1(Native Method)