我正在使用 JPA 创建一个简单的春季启动程序,以显示学生和电话号码关系。
CODE
学生实体
package student.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
@Entity
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long id;
String name;
int roll;
@OneToOne
PhoneNumber num;
public Student() {
super();
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public int getRoll() {
return roll;
}
public PhoneNumber getNum() {
return num;
}
public void setId(Long id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setRoll(int roll) {
this.roll = roll;
}
public void setNum(PhoneNumber num) {
this.num = num;
}
}
电话号码实体
package student.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
@Entity
public class PhoneNumber {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
Long id;
String num;
String type;
@OneToOne
Student stu;
public PhoneNumber() {
super();
}
public PhoneNumber(String num, String type) {
super();
this.num = num;
this.type = type;
}
public Long getId() {
return id;
}
public String getNum() {
return num;
}
public void setId(Long id) {
this.id = id;
}
public void setNum(String num) {
this.num = num;
}
public Student getStu() {
return stu;
}
public void setStu(Student stu) {
this.stu = stu;
}
}
学生和电话号码回购
package student.repo;
import org.springframework.data.repository.CrudRepository;
import student.entity.Student;
public interface StudentRepo extends CrudRepository<Student, Long> {
}
package student.repo;
import org.springframework.data.repository.CrudRepository;
import student.entity.PhoneNumber;
public interface PhoneNumberRepo extends CrudRepository<PhoneNumber, Long>{
}
SeedFile输入虚拟数据
package student;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import student.entity.PhoneNumber;
import student.entity.Student;
import student.repo.PhoneNumberRepo;
import student.repo.StudentRepo;
@Component
public class SeedDb implements CommandLineRunner {
private StudentRepo studentRepo;
private PhoneNumberRepo phoneNumberRepo;
public SeedDb(StudentRepo studentRepo, PhoneNumberRepo phoneNumberRepo) {
super();
this.studentRepo = studentRepo;
this.phoneNumberRepo = phoneNumberRepo;
}
@Override
public void run(String... args) throws Exception {
System.out.println("######################");
Student stu1 = new Student();
stu1.setName("X");
stu1.setRoll(4);
PhoneNumber p = new PhoneNumber("9090909090","Home");
phoneNumberRepo.save(p); //saving phone number to database
stu1.setNum(p);
studentRepo.save(stu1);
}
}
在此程序中(在seedDb文件中),我必须先使用 phoneNumberRepo 手动保存电话号码,然后再将其设置为学生,有在线教程,不需要这一步。
所示请告诉我如何将 PhoneNumber 设置为学生而不将其保存到数据库,即当我保存学生时, phoneNumber < / strong>自动保存到数据库以及如何自动将学生的ID设置为电话号码。
答案 0 :(得分:0)
您的代码中的问题是您在学生中设置了PhoneNumber,但您从未在您的PhoneNumber中设置学生。
对于双向关系,您需要在您创建关系的两个实体之一的cascade
注释中使用@OneToOne
参数。拇指的规则是,应该从父母到孩子进行级联。在您的情况下,Student是父类,因此您应该执行以下操作:
@OneToOne(cascade = CascadeType.ALL)
PhoneNumber num;
创建一个像这样的学生:
Student student = new Student();
student.setName("John Doe");
student.setRoll(4);
PhoneNumber phoneNumber = new PhoneNumber("9090909090", "Home");
student.setNum(phoneNumber);
phoneNumber.setStu(student);
studentRepository.save(student);
结果: