При использовании onetomany
сопоставления с mappedby
я получаю null
в столбце uid
таблицы vehicle
.
Один User
может иметь много vehicle
с, поэтомуOneToMany
отображение используется ..
Vehicle
таблица должна содержать ссылку на user table id
..
Здесь я получаю null
в столбце uid
, когда Vehicle
таблицаЭто должно содержать значение user id
.Без mappedby
работает нормально.
@Entity
@Table(name="User_Details")
public class UserDetails {
private long uid;
@Column(name="fname")
private String name;
private Set<Vehicle> vehicle=new HashSet<Vehicle>();
public UserDetails() {
}
public UserDetails(String name) {
this.name = name;
}
@Id
@Column(name="uid")
@GeneratedValue
public long getUid() {
return uid;
}
public void setUid(long uid) {
this.uid = uid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@OneToMany(mappedBy="user",cascade = CascadeType.ALL)
public Set<Vehicle> getVehicle() {
return vehicle;
}
public void setVehicle(Set<Vehicle> vehicle) {
this.vehicle = vehicle;
}
}
@Entity
@Table(name="Vehicle")
public class Vehicle {
private long id;
@Column(name="vname")
private String name;
private UserDetails user;
public Vehicle() {
}
public Vehicle(String name, UserDetails user) {
super();
this.name = name;
this.user = user;
}
@Id
@Column(name="vid")
@GeneratedValue
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@ManyToOne
@JoinColumn(name = "uid") **References UserDetails table**
public UserDetails getUser() {
return user;
}
public void setUser(UserDetails user) {
this.user = user;
}
}
public class App {
public static void main(String\[\] args) {
SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
Session session=sessionFactory.openSession();
session.beginTransaction();
UserDetails user=new UserDetails();
user.setName("Rohit");
Vehicle vehicle1=new Vehicle();
vehicle1.setName("Hyundai1");
Vehicle vehicle2=new Vehicle();
vehicle2.setName("Maruti1");
Set<Vehicle> set = new HashSet<Vehicle>();
set.add(vehicle1);
set.add(vehicle2);
user.setVehicle(set);
session.save(user);
session.getTransaction().commit();
session.close();
}**][1]][1]
[1]: https://i.stack.imgur.com/YyhsN.png