在 Hibernate 双向 ManytoOne、OnetoMany 的映射列中获取 null

Posted

技术标签:

【中文标题】在 Hibernate 双向 ManytoOne、OnetoMany 的映射列中获取 null【英文标题】:Getting null in mapping column in Hibernate bidirectional ManytoOne , OnetoMany 【发布时间】:2019-05-22 21:54:03 【问题描述】:

我正在尝试创建双向 OnetoMany、ManytoOne 映射。下面是实体类 StudentAddress 和 Student3 类:

Student3实体类:

@Entity
@Table(name="Student3", schema="cf_2583f365_c3c6_499a_a60d_138e7e7023eb")
public class Student3 
@Id
@Column(name = "STUDENT_ID")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int student_id;

@Column(name = "STUDENT_NAME")
private String student_name;

@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="student_address_id",referencedColumnName="address_id")
/*@JoinColumn(name = "student_address_id")*/
private StudentAddress studentAddress;


@Override
public String toString() 
    return "Student3 [student_id=" + student_id + ", STUDENT_NAME=" + student_name + "]";


public int getStudent_id() 
    return student_id;


public void setStudent_id(int student_id) 
    this.student_id = student_id;


public String getStudent_name() 
    return student_name;


public void setStudent_name(String student_name) 
    this.student_name = student_name;


public StudentAddress getStudentAddress() 
    return studentAddress;


public void setStudentAddress(StudentAddress studentAddress) 
    this.studentAddress = studentAddress;


public Student3() 




学生地址实体类:

@Entity
@Table(name="StudentAddress", schema="cf_2583f365_c3c6_499a_a60d_138e7e7023eb")
public class StudentAddress 

@Id
@Column(name = "address_id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int address_id;

@Column(name = "address_detail")
private String address_detail;

@OneToMany(cascade=CascadeType.ALL,mappedBy = "studentAddress")
private List<Student3> students;



@Override
public String toString() 
    return "StudentAddress [address_id=" + address_id + ", address_detail=" + address_detail + ", students="
            + students + "]";


public int getAddress_id() 
    return address_id;


public void setAddress_id(int address_id) 
    this.address_id = address_id;


public String getAddress_detail() 
    return address_detail;


public void setAddress_detail(String address_detail) 
    this.address_detail = address_detail;


public List<Student3> getStudents() 
    return students;


public void setStudents(List<Student3> students) 
    this.students = students;



public StudentAddress() 




在保存 Student3 对象的数据时,我可以像这样保存数据:

但是当我从 StudentAddress 端保存数据时,我在 student_address_id 列中得到一个空值:

从 Student3,studentAddress 端保存数据的代码:

/**************SAVE STUDENT Address Controller*************************/
@RequestMapping(value="/saveStudentAddress", method=RequestMethod.POST,consumes=MediaType.APPLICATION_JSON_VALUE)
public String savestudentaddress (@RequestBody StudentAddress s1) 

    System.out.println(s1);
    studentaddressRepository.save(s1);
    return "success";


/**************SAVE STUDENT3 Controller*************************/
@RequestMapping(value="/saveStudent3", method=RequestMethod.POST,consumes=MediaType.APPLICATION_JSON_VALUE)
public String savestudent (@RequestBody Student3 s1) 

    System.out.println(s1);
    student3Repository.save(s1);
    return "success";

从 Student3 端保存数据的 JSON:


    "student_id" :123, 
    "student_name": "avijit kumar Mamod",

    "studentAddress":
                    "address_id" : 123,
                    "address_detail" : "209 - Berlington Street"
                               

从 StudentAddress 端保存数据的 Json:


"address_id" : 123,
"address_detail" : "209 - Berlington Street",
"students": [
            "student_id" :123, 
            "student_name": "avijit kumar Mamod"
           ]

【问题讨论】:

您能否展示一下您如何从 StudentAddress 端保存数据的代码? @Abhi 我已经更新了我的 qn ,添加了相同的代码。 【参考方案1】:

今天,从StudentAddress 一侧删除mappedBy 并放入@JoinColumn(name="student_address_id"),当我尝试从studentAddress 一侧保存时,它确实在DB student_address_id 中增加了价值。

studentAddress 现在看起来像这样:

@OneToMany(cascade=CascadeType.ALL/*,mappedBy = "studentAddress"*/)
@JoinColumn(name="student_address_id")
private List<Student3> students;

我不知道为什么会这样。我没有上面答案中abhi建议的额外代码。控制器现在很简单:

@RequestMapping(value="/saveStudentAddress", method=RequestMethod.POST,consumes=MediaType.APPLICATION_JSON_VALUE)
public String savestudentaddress (@RequestBody StudentAddress s1) 
    System.out.println(s1);
   studentaddressRepository.save(s1);
    return "success";

【讨论】:

【参考方案2】:

修改您的 saveStudentAddress 代码,如下所示并尝试

@RequestMapping(value="/saveStudentAddress", method=RequestMethod.POST,consumes=MediaType.APPLICATION_JSON_VALUE)
public String savestudentaddress (@RequestBody StudentAddress s1) 
    System.out.println(s1);

    for(Student s: s1.getStudents())
      s.setAddress(s1);
    

    studentaddressRepository.save(s1);
    return "success";

【讨论】:

它的工作!但我不明白,我们甚至没有保存's',我们还在保存's1'。为什么设置 s, wud 会有这么大的不同?? 这就是 Hibernate 文档在这种情况下建议保存对象的方式。希望它有所帮助。 请看下面我的回答,这更离奇【参考方案3】:

不要在两个类中都写 OneToMany。

【讨论】:

我已经编辑了我的问题,我错误地添加了 StudentAddress 类两次。现在Student3课已经上线了。

以上是关于在 Hibernate 双向 ManytoOne、OnetoMany 的映射列中获取 null的主要内容,如果未能解决你的问题,请参考以下文章

带有连接表的双向@ManyToOne 创建重复键

JPA(Hibernate)上的多个双向映射问题

双向 JPA OneToMany/ManyToOne 关联中的“关联的反面”是啥?

为啥在这个 Hibernate 映射中使用 @ManyToOne 而不是 @OneToOne?

org.hibernate.PersistentObjectException:分离的实体传递给坚持@ManyToOne

Java JPA双向ManyToOne映射不起作用