一、hbm.xml
1.SXTeacherEntityMTM.hbm.xml
1 <?xml version='1.0' encoding='utf-8'?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping package="com.sdkj.hibernate.domain"> 6 7 <class name="SXTeacherEntity_Many_To_Many" table="sxmtm_teacher" schema="678"> 8 <id name="tid" type="int"> 9 <column name="tid"></column> 10 <generator class="native"></generator> 11 </id> 12 <property name="tname" column="tname"/> 13 14 <set name="courseEntities" table="teacher_course"> 15 <key> 16 <column name="tid"/> 17 </key> 18 <many-to-many class="CourseEntity_Many_To_Many" column="cid"></many-to-many> 19 </set> 20 </class> 21 </hibernate-mapping>
2.CourseEntityMTM.hbm.xml
1 <?xml version='1.0' encoding='utf-8'?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping package="com.sdkj.hibernate.domain"> 6 <class table="mtm_course" name="CourseEntity_Many_To_Many" schema="678"> 7 <id name="cid"> 8 <column name="cid"></column> 9 <generator class="native"></generator> 10 </id> 11 <property name="cname" column="cname"></property> 12 13 <set name="tset" table="teacher_course" inverse="true"><!-- 多对多的一方关闭维护中间表关系 --> 14 <key> 15 <column name="cid"/> 16 </key> 17 <many-to-many column="tid" class="SXTeacherEntity_Many_To_Many"></many-to-many> 18 </set> 19 </class> 20 </hibernate-mapping>
二、TestSXManyToMany
1 package com.sdkj.hibernate.controller; 2 3 import com.sdkj.hibernate.domain.CourseEntity_Many_To_Many; 4 import com.sdkj.hibernate.domain.SXTeacherEntity_Many_To_Many; 5 import com.sdkj.hibernate.domain.TeacherEntity_Many_To_Many; 6 import com.sdkj.hibernate.util.CommonUtil; 7 import org.hibernate.Session; 8 import org.hibernate.SessionFactory; 9 import org.hibernate.Transaction; 10 import org.junit.jupiter.api.Test; 11 12 import java.util.HashSet; 13 14 /** 15 * @Author wangshuo 16 * @Date 2022/4/9, 10:42 17 * Please add a comment 18 */ 19 public class TestSXManyToMany extends CommonUtil { 20 21 @Test 22 public void test(){ 23 24 SessionFactory sessionFactory = getSessionFactory(); 25 Session session = sessionFactory.openSession(); 26 Transaction transaction = session.beginTransaction(); 27 28 SXTeacherEntity_Many_To_Many teacherEntity_many_to_many = new SXTeacherEntity_Many_To_Many(); 29 teacherEntity_many_to_many.setTname("郭老师"); 30 SXTeacherEntity_Many_To_Many teacherEntity_many_to_many1 = new SXTeacherEntity_Many_To_Many(); 31 teacherEntity_many_to_many1.setTname("王二"); 32 33 CourseEntity_Many_To_Many courseEntity_many_to_many = new CourseEntity_Many_To_Many(); 34 courseEntity_many_to_many.setCname("体育"); 35 36 HashSet<SXTeacherEntity_Many_To_Many> set = new HashSet<>(); 37 set.add(teacherEntity_many_to_many); 38 set.add(teacherEntity_many_to_many1); 39 courseEntity_many_to_many.setTset(set); 40 41 HashSet<CourseEntity_Many_To_Many> set1 = new HashSet<>(); 42 set1.add(courseEntity_many_to_many); 43 teacherEntity_many_to_many1.setCourseEntities(set1); 44 teacherEntity_many_to_many.setCourseEntities(set1); 45 46 session.save(teacherEntity_many_to_many1); 47 session.save(teacherEntity_many_to_many); 48 session.save(courseEntity_many_to_many); 49 50 commit(transaction,sessionFactory,session); 51 } 52 }
本文来自博客园,作者:荣慕平,转载请注明原文链接:https://www.cnblogs.com/rongmuping/articles/16121308.html