日期:2014-05-17  浏览次数:20563 次

hibernate需要双向存储吗?manytomany的时候
Java code

@Entity
class Student{
long 
@ManyToMany
Set<Course> courses;
}

@Entity
class Course{
@ManyToMany
Set<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呢?

有些迷拉哈。





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

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