Springboot JPA关系Map错误

31moq8wy  于 2023-11-19  发布在  Spring
关注(0)|答案(1)|浏览(141)

你好,我是新来的Springboot JPA.我试图了解实体之间的关系Map.我有4个实体,学生,地址,教师和学校.我得到多个错误,最近被分离实体传递到持久化,同时插入数据到实体.我想我没有做正确的Map.请帮助我

public class Student {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "student_id_seq")
    @SequenceGenerator(name ="student_id_seq", allocationSize = 1, sequenceName = "student_id_seq",initialValue = 1)
    private long studentId;
    private String name;
    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name="address_id",referencedColumnName = "id")
    private Address address;
    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name="teacher_id")
    private Teacher teacher;

    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "school_uuid", referencedColumnName = "school_uuid")
    private School school;
}

public class Address {
    @Id
    @GeneratedValue
    private int id;
    private String addressLine;
    @OneToOne(mappedBy = "address")
    private Student student;
}

public class School {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long schoolId;
    @Column(unique = true)
    @GeneratedValue(strategy = GenerationType.UUID)
    private UUID school_uuid;
    private String schoolName;

    @OneToMany(mappedBy = "school")
    @JsonManagedReference
    private List<Teacher> teachers;
    @OneToMany(mappedBy = "school")
    private List<Student> students;

    @PrePersist
    public void prePersistToUpdateUUid(){
        System.out.println("prepersist called");
        if(this.school_uuid == null){
            this.setSchool_uuid(UUID.randomUUID());
        }
    }
}

public class Teacher {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "teacher_id_seq")
    @SequenceGenerator(name = "teacher_id_seq", sequenceName = "teacher_id_seq", allocationSize = 1, initialValue = 1)
    private long teacherId;
    private String teacherName;
    private String course;
    @ManyToOne
    @JoinColumn(name="school_uuid", referencedColumnName = "school_uuid")
    @JsonBackReference
    private School school;
    @OneToMany(mappedBy = "teacher")
    private List<Student> students;

}

字符串
尝试持久化学生实体并出错
org.springframework.dao. InvalidDataBase ApiUsageException:无法使用
实体传递到持久化:

public Student addStudent(Student student) {
        // set school name if it exists and teacher name if she exists

        School school = schoolRepository.findByschoolName(student.getSchool().getSchoolName());
        if(school!=null){
            student.getSchool().setSchool_uuid(school.getSchool_uuid());;
        }

        Teacher teacher=teacherRepository.findByteacherName(student.getTeacher().getTeacherName());
        if(teacher!=null){
            student.getTeacher().setTeacherId(teacher.getTeacherId());
        }
return studentRepository.save(student);
    }

mi7gmzs6

mi7gmzs61#

您必须设置teacherschool本身,而不仅仅是id

public Student addStudent(Student student) {
        // set school name if it exists and teacher name if she exists

        School school = schoolRepository.findByschoolName(student.getSchool().getSchoolName());
        student.setSchool(school);

        Teacher teacher=teacherRepository.findByteacherName(student.getTeacher().getTeacherName());
        student.setTeacher(teacher);
return studentRepository.save(student);
    }

字符串

相关问题