I have recently started studying Hibernate and was working with mappings. In my scenario, I have a user which can have multiple vehicles. While doing a bidirectional mapping, the column that was getting generated in the second table because if this bidirectional mapping, I am getting null values. I tried debugging but of no use.
UserDetails.java
package org.hibernate.OneToMany;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table (name = "USER_DETAILS")
public class UserDetails {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "USER_ID")
private int userId;
@Column(name = "USER_NAME")
private String userName;
@OneToMany(mappedBy="user")
private Collection<Vehicle> vehicle = new ArrayList<Vehicle>();
public Collection<Vehicle> getVehicle() {
return vehicle;
}
public void setVehicle(Collection<Vehicle> vehicle) {
this.vehicle = vehicle;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
Vehicle.java
package org.hibernate.OneToMany;
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;
}
}
HibernateTestOneToMany.java
package org.hibernate.OneToMany;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateTestOneToMany {
public static void main(String[] args) {
UserDetails user = new UserDetails();
user.setUserName("First User");
Vehicle vehicle = new Vehicle();
vehicle.setVehicleName("Audi");
Vehicle vehicle2 = new Vehicle();
vehicle2.setVehicleName("Jeep");
user.getVehicle().add(vehicle);
user.getVehicle().add(vehicle2);
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(user);
session.save(vehicle);
session.save(vehicle2);
session.getTransaction().commit();
session.close();
sessionFactory.close();
}
}
output:
Also I was finding it a bit difficult to understand as to why we should prefer bidirectional to unidirectional mapping.
You have to set the user inside the Vehicle object you are instantiating inside the test.