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

asked on

hibernate many to one

I am trying hibernate many to one example as below

I have many vehicles like car and jeep etc which has only 1 user



hibernate cfg xml class 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"/>
</session-factory>
</hibernate-configuration>


hibernate client java class looks like below



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 HibernateTestManyOne {

public static void main(String[] args){
      UserDetails user=new UserDetails();
      //user.setUserId(9);
      user.setUserName("1stth User");
      
      
      Vehicle vehicle=new Vehicle();
      vehicle.setVehicleName("car");
      
      Vehicle vehicle2=new Vehicle();
      vehicle2.setVehicleName("jeep");
      
      user.getVehicle().add(vehicle);
      user.getVehicle().add(vehicle2);
      
      vehicle.setUser(user);
      vehicle2.setUser(user);

      SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
      Session session=sessionFactory.openSession();
      session.beginTransaction();
      session.save(user);
      session.save(vehicle);
      session.save(vehicle2);
      //session.save(user2);
      session.getTransaction().commit();
      session.close();

      
}
}


vehicle.java looks like below


package org.gp.gpr.dto;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;

@Entity
public class Vehicle {
      @Id@GeneratedValue
private int vehicleId;
private String vehicleName;
@ManyToOne
private UserDetails user;
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;
}

}


UseerDetails.java looks like below

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.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Lob;
import javax.persistence.OneToMany;
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(strategy=GenerationType.AUTO)
private int userId;
private String userName;
@OneToMany
@JoinTable(joinColumns=@JoinColumn(name="USER_ID"),
inverseJoinColumns=@JoinColumn(name="VEHICLE_Id")
)
private Collection<Vehicle> vehicle=new ArrayList<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 Collection<Vehicle> getVehicle() {
      return vehicle;
}
public void setVehicle(Collection<Vehicle> vehicle) {
      this.vehicle = vehicle;
}
/*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;
}*/




}






I have modified UserDetails.java and Vehicle.java much further to eliminate joining table




vehicle.java
package org.gp.gpr.dto;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Entity
public class Vehicle {
      @Id@GeneratedValue
private int vehicleId;
private String vehicleName;
@ManyToOne
@JoinColumn(name="USER_ID")
private UserDetails user;
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;
}

}



UserDetails.java 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.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Lob;
import javax.persistence.OneToMany;
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(strategy=GenerationType.AUTO)
private int userId;
private String userName;
@OneToMany(mappedBy="user")
//@JoinTable(joinColumns=@JoinColumn(name="USER_ID"),
//inverseJoinColumns=@JoinColumn(name="VEHICLE_Id")
//)
private Collection<Vehicle> vehicle=new ArrayList<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 Collection<Vehicle> getVehicle() {
      return vehicle;
}
public void setVehicle(Collection<Vehicle> vehicle) {
      this.vehicle = vehicle;
}
/*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;
}*/




}





i would like to know how to see the user of the vehicle car also how to see uyser of the vehicle jeep.

Please advise. Any ideas, resources, sample code highly appreciated. thanks in advance
manytoOne.jpg
manytoOneee2.jpg
manytoOneee3.jpg
manytoOneee4.jpg
ASKER CERTIFIED SOLUTION
Avatar of Plk_In_EE
Plk_In_EE
Flag of India image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account