gudii9
asked on
hibernate joined strategy uses
Hi,
I was working on hibernate joined strategy as below
My question is what is advantage of joined strategy as the data inserted is not complete in each table and we have to go for join to see the complete data.
Please advise. Any ideas, resources, sample code highly appreciated. thanks in advance
joined1.jpg
joined2.jpg
joined3.jpg
joined4.jpg
joined5.jpg
I was working on hibernate joined strategy as below
hibernate.cfg.xml looks like below
<?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>
hibernate client java file 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.java 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.JOINED)
//@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 class looks like below
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;
}
}
FourWheeler class looks like below
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;
}
}
My question is what is advantage of joined strategy as the data inserted is not complete in each table and we have to go for join to see the complete data.
Please advise. Any ideas, resources, sample code highly appreciated. thanks in advance
joined1.jpg
joined2.jpg
joined3.jpg
joined4.jpg
joined5.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.