gpt4 book ai didi

java - 在 Hibernate 双向 ManytoOne 和 OnetoMany 的映射列中获取空值

转载 作者:行者123 更新时间:2023-11-29 04:13:09 26 4
gpt4 key购买 nike

我正在尝试创建双向 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";
}

从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"
}]
}

最佳答案

像下面这样修改你的 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";
}

关于java - 在 Hibernate 双向 ManytoOne 和 OnetoMany 的映射列中获取空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53901497/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com