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

asked on

hibernate one to one

Hi,

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_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"/>
</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().buildSessionFactory();
      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.CollectionId;
import org.hibernate.annotations.GenericGenerator;
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_Id")
)
      
private Collection<Address> listOfAddresses=new ArrayList<Address>();

public Collection<Address> getListOfAddresses() {
      return listOfAddresses;
}
public void setListOfAddresses(Collection<Address> 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
Avatar of Plk_In_EE
Plk_In_EE
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
ASKER CERTIFIED SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.