java 双向OneToMany和ManyToOne给出null

whhtz7ly  于 2024-01-05  发布在  Java
关注(0)|答案(2)|浏览(173)

我最近开始学习Hibernate并使用Map。在我的场景中,我有一个可以拥有多辆车的用户。在进行双向Map时,在第二个表中生成的列,因为如果这个双向Map,我会得到空值。我尝试调试但没有用。
UserDetails.java

  1. package org.hibernate.OneToMany;
  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. import javax.persistence.Column;
  5. import javax.persistence.Entity;
  6. import javax.persistence.GeneratedValue;
  7. import javax.persistence.GenerationType;
  8. import javax.persistence.Id;
  9. import javax.persistence.OneToMany;
  10. import javax.persistence.Table;
  11. @Entity
  12. @Table (name = "USER_DETAILS")
  13. public class UserDetails {
  14. @Id
  15. @GeneratedValue(strategy=GenerationType.AUTO)
  16. @Column(name = "USER_ID")
  17. private int userId;
  18. @Column(name = "USER_NAME")
  19. private String userName;
  20. @OneToMany(mappedBy="user")
  21. private Collection<Vehicle> vehicle = new ArrayList<Vehicle>();
  22. public Collection<Vehicle> getVehicle() {
  23. return vehicle;
  24. }
  25. public void setVehicle(Collection<Vehicle> vehicle) {
  26. this.vehicle = vehicle;
  27. }
  28. public int getUserId() {
  29. return userId;
  30. }
  31. public void setUserId(int userId) {
  32. this.userId = userId;
  33. }
  34. public String getUserName() {
  35. return userName;
  36. }
  37. public void setUserName(String userName) {
  38. this.userName = userName;
  39. }
  40. }

字符串
Vehicle.java

  1. package org.hibernate.OneToMany;
  2. import javax.persistence.Entity;
  3. import javax.persistence.GeneratedValue;
  4. import javax.persistence.Id;
  5. import javax.persistence.JoinColumn;
  6. import javax.persistence.ManyToOne;
  7. @Entity
  8. public class Vehicle {
  9. @Id
  10. @GeneratedValue
  11. private int vehicleId;
  12. private String vehicleName;
  13. @ManyToOne
  14. @JoinColumn(name ="USER_ID")
  15. private UserDetails user;
  16. public UserDetails getUser() {
  17. return user;
  18. }
  19. public void setUser(UserDetails user) {
  20. this.user = user;
  21. }
  22. public int getVehicleId() {
  23. return vehicleId;
  24. }
  25. public void setVehicleId(int vehicleId) {
  26. this.vehicleId = vehicleId;
  27. }
  28. public String getVehicleName() {
  29. return vehicleName;
  30. }
  31. public void setVehicleName(String vehicleName) {
  32. this.vehicleName = vehicleName;
  33. }
  34. }


HibernateTestOneToMany.java

  1. package org.hibernate.OneToMany;
  2. import org.hibernate.Session;
  3. import org.hibernate.SessionFactory;
  4. import org.hibernate.cfg.Configuration;
  5. public class HibernateTestOneToMany {
  6. public static void main(String[] args) {
  7. UserDetails user = new UserDetails();
  8. user.setUserName("First User");
  9. Vehicle vehicle = new Vehicle();
  10. vehicle.setVehicleName("Audi");
  11. Vehicle vehicle2 = new Vehicle();
  12. vehicle2.setVehicleName("Jeep");
  13. user.getVehicle().add(vehicle);
  14. user.getVehicle().add(vehicle2);
  15. SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
  16. Session session = sessionFactory.openSession();
  17. session.beginTransaction();
  18. session.save(user);
  19. session.save(vehicle);
  20. session.save(vehicle2);
  21. session.getTransaction().commit();
  22. session.close();
  23. sessionFactory.close();
  24. }
  25. }


产出:


的数据
另外,我发现它有点难以理解,为什么我们应该更喜欢双向Map,而不是单向Map。

waxmsbnn

waxmsbnn1#

您必须在测试中示例化的Vehicle对象中设置用户。

mum43rcc

mum43rcc2#

如果有人使用spring Boot :我也尝试使用双向方法填充外键列,但我只得到null。如果我使用@OneToMany annotation,那么默认情况下会使用单向方法创建一个新表。我使用@JoinColumn对列使用@OneToMany annotation和单向方法,那么我只得到2个正确填充外键列的表。

  1. @OneToMany(cascade = CascadeType.ALL)
  2. @JoinColumn
  3. Set<Vehicle> vs;

字符串

相关问题