hibernate需要双向存储吗?manytomany的时候
- Java code
@Entityclass Student{long @ManyToManySet<Course> courses;}@Entityclass Course{@ManyToManySet<Student> students;}当我 这样时候
- Java code
Course couse1=new Course(); Course couse2=new Course(); Student student1=new Student(); Student student2=new Student(); student1.setCourses(Arrays.asList(couse1,couse2)); student2.setCourses(Arrays.asList(couse1,couse2));
如果save student1,student2的话,这样的时候course1,course2是不是也自动保存啦?也能导向 student1, student2呢?
有些迷拉哈。
[解决办法]
多对多一般是通过中间表来关联比较好。
[解决办法]
一般单向关联存储就可以了,双向可能造成重复
当然,也可以像楼上说的,如果是多对多最好搞个中间表。