gudii9
asked on
hibernate one to one
Hi,
I am running below hibernate one to one example
my question is when i ran the java client application output looks as below
Hibernate: select nextval ('hibernate_sequence')
Hibernate: select nextval ('hibernate_sequence')
Hibernate: insert into USER_DETAILS (userName, VEHICLE_Id, userId) values (?, ?, ?)
Hibernate: insert into Vehicle (vehicleName, vehicleId) values (?, ?)
Hibernate: update USER_DETAILS set userName=?, VEHICLE_Id=? where userId=?
I wonder why below line still shows vehicleid instead of VEHICLE_Id as below
>>Hibernate: insert into Vehicle (vehicleName, vehicleId) values (?, ?)
instead of as below
>>Hibernate: insert into Vehicle (vehicleName, VEHICLE_Id) values (?, ?)
Please advise. Any ideas, resources, sample code highly appreciated. thanks in advance
HibernateOneOne.jpg
I am running below hibernate one to one example
hibernate config file 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_cl ass">org.p ostgresql. Driver</pr operty>
<property name="hibernate.connection.url">jdbc :postgresq l://localh ost:5432/h ibernatedb </property >
<property name="hibernate.connection.username" >postgres< /property>
<property name="hibernate.connection.password" >root</pro perty>
<property name="hibersnate.connection.pool_siz e">10</pro perty>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect .PostgreSQ LDialect</ property>
<property name="hibernate.hbm2ddl.auto">create </property >
<mapping class="org.gp.gpr.dto.UserDetails"/>
<mapping class="org.gp.gpr.dto.Vehicle"/>
</session-factory>
</hibernate-configuration>
hibernate client class looks like
package org.gp.gpr.hibernate;
import java.util.Date;
import org.gp.gpr.dto.Address;
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 HibernateTestOneOne {
public static void main(String[] args){
UserDetails user=new UserDetails();
//user.setUserId(9);
user.setUserName("1stth User");
Vehicle vehicle=new Vehicle();
vehicle.setVehicleName("car");
user.setVehicle(vehicle);
SessionFactory sessionFactory = new Configuration().configure().buildSes sionFactor y();
Session session=sessionFactory.openSession() ;
session.beginTransaction();
session.save(user);
session.save(vehicle);
//session.save(user2);
session.getTransaction().commit();
session.close();
}
}
vehicle model class looks like
package org.gp.gpr.dto;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
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;
}
}
UserDetails model class looks like
package org.gp.gpr.dto;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides ;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Lob;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import org.hibernate.annotations.Collection Id;
import org.hibernate.annotations.GenericGen erator;
import org.hibernate.annotations.Type;
//import org.hibernate.annotations.Entity;
@Entity
@Table(name="USER_DETAILS")
public class UserDetails {
@Id @GeneratedValue
private int userId;
//@Transient
private String userName;
@OneToOne
@JoinColumn(name="VEHICLE_Id")
private Vehicle vehicle;
/*@ElementCollection
@JoinTable(name="USER_ADDRESS",
joinColumns=@JoinColumn(name="USER_I d")
)
private Collection<Address> listOfAddresses=new ArrayList<Address>();
public Collection<Address> getListOfAddresses() {
return listOfAddresses;
}
public void setListOfAddresses(Collection<Addres s> listOfAddresses) {
this.listOfAddresses = listOfAddresses;
}
*/
/*public void setAddress(Address address) {
this.address = address;
}*/
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName+"from get";
}
public Vehicle getVehicle() {
return vehicle;
}
public void setVehicle(Vehicle vehicle) {
this.vehicle = vehicle;
}
public void setUserName(String userName) {
this.userName = userName;
}
/*public Set<Address> getListOfAddresses() {
return listOfAddresses;
}
public void setListOfAddresses(Set<Address> listOfAddresses) {
this.listOfAddresses = listOfAddresses;
}*/
}
my question is when i ran the java client application output looks as below
Hibernate: select nextval ('hibernate_sequence')
Hibernate: select nextval ('hibernate_sequence')
Hibernate: insert into USER_DETAILS (userName, VEHICLE_Id, userId) values (?, ?, ?)
Hibernate: insert into Vehicle (vehicleName, vehicleId) values (?, ?)
Hibernate: update USER_DETAILS set userName=?, VEHICLE_Id=? where userId=?
I wonder why below line still shows vehicleid instead of VEHICLE_Id as below
>>Hibernate: insert into Vehicle (vehicleName, vehicleId) values (?, ?)
instead of as below
>>Hibernate: insert into Vehicle (vehicleName, VEHICLE_Id) values (?, ?)
Please advise. Any ideas, resources, sample code highly appreciated. thanks in advance
HibernateOneOne.jpg
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.