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

时间:2018-12-23 05:47:03

标签: java hibernate spring-boot hibernate-mapping

我正在尝试创建双向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对象保存数据时,我可以这样保存数据:

enter image description here

enter image description here

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

enter image description here

enter image description here

用于保存来自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";
}

Json从Student3端保存数据:

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

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

Json从StudentAddress端保存数据:

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

3 个答案:

答案 0 :(得分: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";
}

答案 1 :(得分:1)

现在,现在,当从StudentAddress端删除maptedBy并放置@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 :(得分:0)

不要在两个类中都编写OneToMany。