一个Teacher 一个Student 表,多对多关系,关联表Teacher_Student
想要实现下面这个功能该怎么配置呢
1、创建一条Teacher的数据记录,并在Teacher_Student中间表插入关联数据,但是Student数据是不需要操作的。
2、根据TeacherID 删除Teacher_Student中间表数据。
自己找了好久,没有找到合适的资料,尝试着不通的配置也没有找到出路,
部分代码如下,但是
Teacher类:Annotation
@ManyToMany(cascade = CascadeType.REFRESH, fetch = FetchType.LAZY)
@JoinTable(name = "Teacher_Student",
joinColumns = { @JoinColumn(name = "TeacherID", referencedColumnName = "TeacherID") },
inverseJoinColumns = { @JoinColumn(name = "StudentID", referencedColumnName = "StudentID") })
Student类:Annotation
@ManyToMany(cascade = CascadeType.REFRESH, fetch = FetchType.LAZY, mappedBy = "teachers")
------解决方案--------------------
- Java code
long[] studentIds = 。。。List<Student> list = new ArrayList<Student>();for(long id : studentIds){ list.add(em.find(Student.class,id);}teacher.setStudents(list);