Hibernate多对多的案列(学生和老师)

多对多 学生和老师的问题就是明显的多对多问题,这里我们采用学生控制老师,即保存学生的时候保存老师,所以在学生里面设置一个cascade即可。但是由于是多对多问题,所以我们还是要建立一个中间表来存放两方的外键。
学生代码(注解配置):

package manytomany; import java.util.HashSet; import java.util.Set; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; import javax.persistence.Table; import org.hibernate.annotations.Cascade; import org.hibernate.annotations.CascadeType; @Entity @Table(name="t_student") public class Student { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; private String name; @ManyToMany(targetEntity=Teacher.class) //使用jointable来描述中间表,并且描述中间表中外键与student,teacher的关系 //joinColumns用来描述student与中间表的映射关系 //inverseJoinColumns用来描述teacher与中间表的映射关系 @JoinTable(name="s_t",joinColumns={@JoinColumn(name="student_id")},inverseJoinColumns={@JoinColumn(name="teacher_id")}) @Cascade(CascadeType.ALL) private Setteachers=new HashSet(); public Set getTeachers() { return teachers; } public void setTeachers(Set teachers) { this.teachers = teachers; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }

老师代码:
package manytomany; import java.util.HashSet; import java.util.Set; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.ManyToMany; import javax.persistence.Table; import org.hibernate.annotations.GenericGenerator; @Entity @Table(name="t_teacher") public class Teacher { @Id @GenericGenerator(name="myuuid",strategy="uuid") @GeneratedValue(generator="myuuid") private String id; private String name; @ManyToMany(targetEntity=Student.class,mappedBy="teachers") private Set>students=new HashSet>(); public Set> getStudents() { return students; } public void setStudents(Set> students) { this.students = students; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }

【Hibernate多对多的案列(学生和老师)】测试代码:建立两个学生同时创建两个老师,
package manytomany; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.junit.Test; public class testdemo { @Test public void test(){ Configuration configuration=new Configuration().configure(); SessionFactory factory=configuration.buildSessionFactory(); Session session=factory.openSession(); session.beginTransaction(); Teacher t1=new Teacher(); t1.setName("tom"); Teacher t2=new Teacher(); t1.setName("fox"); Student s1=new Student(); s1.setName("张三"); Student s2=new Student(); s2.setName("王五"); s1.getTeachers().add(t1); s1.getTeachers().add(t2); s2.getTeachers().add(t1); s2.getTeachers().add(t2); session.save(s1); session.save(s2); session.getTransaction().commit(); session.close(); } }

    推荐阅读