gudii9
asked on
hibernate many to many example issues
I am running below hibernate many to many example.
Like one user can rent many rental vehicles and one rental vehicle can have many users scenario.
Chal;lenge i am facing when running client .java file is mapping table created twice redundantly as below
Hibernate: select nextval ('hibernate_sequence')
Hibernate: select nextval ('hibernate_sequence')
Hibernate: select nextval ('hibernate_sequence')
Hibernate: insert into USER_DETAILS (userName, userId) values (?, ?)
Hibernate: insert into Vehicle (vehicleName, vehicleId) values (?, ?)
Hibernate: insert into Vehicle (vehicleName, vehicleId) values (?, ?)
Hibernate: insert into USER_DETAILS_Vehicle (USER_DETAILS_userId, vehicle_vehicleId) values (?, ?)
Hibernate: insert into USER_DETAILS_Vehicle (USER_DETAILS_userId, vehicle_vehicleId) values (?, ?)
Hibernate: insert into Vehicle_USER_DETAILS (Vehicle_vehicleId, userList_userId) values (?, ?)
Hibernate: insert into Vehicle_USER_DETAILS (Vehicle_vehicleId, userList_userId) values (?, ?)
How do i eliminate that to map only once
Please advise. Any ideas, resources, sample code highly appreciated. thanks in advance
manyMany1.jpg
manyMany2.jpg
manyMany3.jpg
manyMany4.jpg
manyMany5-RedundantMapping.jpg
Like one user can rent many rental vehicles and one rental vehicle can have many users scenario.
hibernate config file 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_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 many to many.java file looks as 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 HibernateTestManyMany {
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.getUserList().add(user);
vehicle2.getUserList().add(user);
SessionFactory sessionFactory = new Configuration().configure().buildSes sionFactor y();
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 class looks as below
package org.gp.gpr.dto;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
@Entity
public class Vehicle {
@Id@GeneratedValue
private int vehicleId;
private String vehicleName;
//@ManyToOne
//@JoinColumn(name="USER_ID")
@ManyToMany
private Collection<UserDetails> userList=new ArrayList();;
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;
}
}
UserDetails.java looks as 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.ManyToMany;
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.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(strategy=GenerationT ype.AUTO)
private int userId;
private String userName;
//@OneToMany(mappedBy="user")
//@JoinTable(joinColumns=@JoinColumn (name="USE R_ID"),
//inverseJoinColumns=@JoinColumn(nam e="VEHICLE _Id")
//)
@ManyToMany
private Collection<Vehicle> vehicle=new ArrayList<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 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;
}*/
}
Chal;lenge i am facing when running client .java file is mapping table created twice redundantly as below
Hibernate: select nextval ('hibernate_sequence')
Hibernate: select nextval ('hibernate_sequence')
Hibernate: select nextval ('hibernate_sequence')
Hibernate: insert into USER_DETAILS (userName, userId) values (?, ?)
Hibernate: insert into Vehicle (vehicleName, vehicleId) values (?, ?)
Hibernate: insert into Vehicle (vehicleName, vehicleId) values (?, ?)
Hibernate: insert into USER_DETAILS_Vehicle (USER_DETAILS_userId, vehicle_vehicleId) values (?, ?)
Hibernate: insert into USER_DETAILS_Vehicle (USER_DETAILS_userId, vehicle_vehicleId) values (?, ?)
Hibernate: insert into Vehicle_USER_DETAILS (Vehicle_vehicleId, userList_userId) values (?, ?)
Hibernate: insert into Vehicle_USER_DETAILS (Vehicle_vehicleId, userList_userId) values (?, ?)
How do i eliminate that to map only once
Please advise. Any ideas, resources, sample code highly appreciated. thanks in advance
manyMany1.jpg
manyMany2.jpg
manyMany3.jpg
manyMany4.jpg
manyMany5-RedundantMapping.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.
ASKER
Hibernate: insert into Vehicle_USER_DETAILS (Vehicle_vehicleId, userList_userId) values (?, ?)
Hibernate: insert into Vehicle_USER_DETAILS (Vehicle_vehicleId, userList_userId) values (?, ?)
please advise