首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

hibernate需要双向存储吗?manytomany的时候解决方法

2012-03-23 
hibernate需要双向存储吗?manytomany的时候Java code@Entityclass Student{long @ManyToManySetCourse c

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呢?

有些迷拉哈。





[解决办法]
多对多一般是通过中间表来关联比较好。
[解决办法]
一般单向关联存储就可以了,双向可能造成重复

当然,也可以像楼上说的,如果是多对多最好搞个中间表。

热点排行