Spring DataJPA不保存第三泰铢到数据库

发布于 2025-01-21 06:21:40 字数 2212 浏览 0 评论 0原文

将我的实体保存到数据库时没有问题。但是,当我尝试将值插入我的第三次多一对一的表中时,它根本没有做任何事情。 这是我的学生实体;

@Table(name = "student", schema = "school")

public class Student {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long studentId;
    @Column(name = "name")
    private String name;
    @Column(name = "surname")
    private String surname;
    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinTable(name = "student_classroom", schema = "school")
    private Collection<Classroom> classroom = new ArrayList<>();
    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
    @JoinTable(name = "student_teacher",
            joinColumns = {
                    @JoinColumn(name = "student_id", referencedColumnName = "studentId",
                            nullable = false, updatable = false)},
            inverseJoinColumns = {
                    @JoinColumn(name = "teacher_id", referencedColumnName = "teacherId",
                            nullable = false, updatable = false)})
    private Set<Teacher> teachers = new HashSet<>();

教师实体。

@Entity
@Table(name = "teacher", schema = "school")
public class Teacher {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long teacherId;
    @Column(name = "name")
    private String name;
    @Column(name = "surname")
    private String surname;
    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinTable(name = "teacher_classroom", schema = "school")
    private Collection<Classroom> classroom = new ArrayList<>();
    @ManyToMany(mappedBy = "teachers", fetch = FetchType.LAZY)
    private Set<Student> students = new HashSet<>();

这是我的服务方法。

public void addTeacherToStudent(long teacherId, long studentId) {
        Optional<Teacher> teacher = teacherRepository.findById(teacherId);
        Optional<Student> student = studentRepository.findById(studentId);
        student.get().getTeachers().add(teacher.get());
        teacher.get().getStudents().add(student.get());
    }

在内存中将老师和学生保存在集合中的情况下没有问题,但是它没有到​​达数据库。我尝试了所有注释,没有工作。

I have no problem with saving my entities to the database. However when I try to insert values into my 3rd many-to-many table, it simply doesn't do anything.
Here is my student entity;

@Table(name = "student", schema = "school")

public class Student {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long studentId;
    @Column(name = "name")
    private String name;
    @Column(name = "surname")
    private String surname;
    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinTable(name = "student_classroom", schema = "school")
    private Collection<Classroom> classroom = new ArrayList<>();
    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
    @JoinTable(name = "student_teacher",
            joinColumns = {
                    @JoinColumn(name = "student_id", referencedColumnName = "studentId",
                            nullable = false, updatable = false)},
            inverseJoinColumns = {
                    @JoinColumn(name = "teacher_id", referencedColumnName = "teacherId",
                            nullable = false, updatable = false)})
    private Set<Teacher> teachers = new HashSet<>();

The teacher entity.

@Entity
@Table(name = "teacher", schema = "school")
public class Teacher {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long teacherId;
    @Column(name = "name")
    private String name;
    @Column(name = "surname")
    private String surname;
    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinTable(name = "teacher_classroom", schema = "school")
    private Collection<Classroom> classroom = new ArrayList<>();
    @ManyToMany(mappedBy = "teachers", fetch = FetchType.LAZY)
    private Set<Student> students = new HashSet<>();

Here is my service method.

public void addTeacherToStudent(long teacherId, long studentId) {
        Optional<Teacher> teacher = teacherRepository.findById(teacherId);
        Optional<Student> student = studentRepository.findById(studentId);
        student.get().getTeachers().add(teacher.get());
        teacher.get().getStudents().add(student.get());
    }

It has no problem saving the teachers and students in the sets in memory, however it doesn't reach the database. I have tried every annotation, didn't work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的他你的她 2025-01-28 06:21:40

尝试将其添加到教师实体中

 @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
 @JoinTable(
            name = "student_teacher",
            joinColumns = @JoinColumn(name = "teacher_id"),
            inverseJoinColumns = @JoinColumn(name = "student_id")
    )
  private Set<Student> students = new HashSet<>();

Try adding this in Teacher entity

 @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
 @JoinTable(
            name = "student_teacher",
            joinColumns = @JoinColumn(name = "teacher_id"),
            inverseJoinColumns = @JoinColumn(name = "student_id")
    )
  private Set<Student> students = new HashSet<>();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文