我正在尝试创建双向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列中会得到一个null:
保存来自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"
}]
}
最佳答案
修改您的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";
}