Spring DataJPA不保存第三泰铢到数据库
将我的实体保存到数据库时没有问题。但是,当我尝试将值插入我的第三次多一对一的表中时,它根本没有做任何事情。 这是我的学生实体;
@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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将其添加到教师实体中
Try adding this in Teacher entity