jdbc:
package demo.test.many2many; import java.util.HashSet; import java.util.Set; public class Student { private int Stu_id; private String Stu_name; private Set<Teacher> teacher = new HashSet<>(); public int getStu_id() { return Stu_id; } public void setStu_id(int stu_id) { Stu_id = stu_id; } public String getStu_name() { return Stu_name; } public void setStu_name(String stu_name) { Stu_name = stu_name; } public Set<Teacher> getTeacher() { return teacher; } public void setTeacher(Set<Teacher> teacher) { this.teacher = teacher; } public Student(int stu_id, String stu_name, Set<Teacher> teacher) { Stu_id = stu_id; Stu_name = stu_name; this.teacher = teacher; } public Student(String stu_name, Set<Teacher> teacher) { Stu_name = stu_name; this.teacher = teacher; } public Student(String stu_name) { Stu_name = stu_name; } @Override public String toString() { return "Student [Stu_id=" + Stu_id + ", Stu_name=" + Stu_name + "]"; } }
package demo.test.many2many; import java.util.HashSet; import java.util.Set; public class Teacher { private int tea_id; private String name; private Set<Student> student = new HashSet<>(); public Teacher() { } public int getTea_id() { return tea_id; } public void setTea_id(int tea_id) { this.tea_id = tea_id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Set<Student> getStudent() { return student; } public void setStudent(Set<Student> student) { this.student = student; } public Teacher(int tea_id, String name, Set<Student> student) { this.tea_id = tea_id; this.name = name; this.student = student; } public Teacher(String name, Set<Student> student) { this.name = name; this.student = student; } public Teacher(String name) { this.name = name; } @Override public String toString() { return "Teacher [tea_id=" + tea_id + ", name=" + name + "]"; } }
.xml文件
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="demo.test.many2many"> <!-- 根据包名找类,根据类名加载对应的类 --> <class name="Student" table="s_student"> <id name="Stu_id" column="stu_id"> <generator class="native"/> </id> <property name="Stu_name" column="Stu_name"/> <!-- t_tea_stu:中间表的表名 当前对象对应的ID列名和中间表 设置中间表的那一列与当前表关联 --> <set name="teacher" table="t_tea_stu"> <key column="id_stu" /><!-- 设置对面的外键 --> <!-- class="对方的类名" column="" --> <many-to-many class="Teacher" column="id_tea"/> </set> </class> </hibernate-mapping>
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="demo.test.many2many"> <!-- 根据包名找类,根据类名加载对应的类 --> <class name="Teacher" table="t_teacher"> <id name="tea_id" column="t_id"> <generator class="native"/> </id> <property name="name" column="t_name"/> <!-- inverse:管理权 ERROR:Duplicate entry '1-2' for key 'PRIMARY' --> <set name="student" table="t_tea_stu" > <key column="id_tea"/> <!-- 设置对面的外键 --> <many-to-many class="Student" column="id_stu"/> </set> </class> </hibernate-mapping>
test测试类:
package demo.test.many2many; import org.hibernate.Session; import org.junit.Test; import demo.test.utiltool.UtilTool; public class Many2Many { //关联关系one2one;one2many;many2one;many2many @Test public void testsave(){ Teacher tea1 = new Teacher("张老师"); Teacher tea2 = new Teacher("刘老师"); Student stu1 = new Student("王同学"); Student stu2 = new Student("李同学"); //将学生添加给老师 tea1.getStudent().add(stu1); tea1.getStudent().add(stu2); tea2.getStudent().add(stu1); tea2.getStudent().add(stu2); Session session = UtilTool.getInstance().getSession(); session.beginTransaction(); session.save(stu1); session.save(stu2); session.save(tea1); session.save(tea2); session.getTransaction().commit(); session.close(); } }
最后,记得将各.hbm.xml文件引入cfg.xml