日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Hibernate从零开始_07_多对多关系(中间表)

發布時間:2025/6/15 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Hibernate从零开始_07_多对多关系(中间表) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、Hibernate多對多(中間表含多個字段)的把多對多拆分為兩個一對多。實現如下:

????學生類:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 /** ?*?學生表 ?*/ public?class?Student?{ ?private?Long?id; ?private?String?name; ?? ?//與學生課程關聯表建立一對多關系 ?private?Set<StudentCourseRelation>?scr?=?new?HashSet<StudentCourseRelation>(); ?? ?public?Student()?{ ?} ?public?Long?getId()?{ ??return?id; ?} ?public?void?setId(Long?id)?{ ??this.id?=?id; ?} ?public?String?getName()?{ ??return?name; ?} ?public?void?setName(String?name)?{ ??this.name?=?name; ?} ?public?Set<StudentCourseRelation>?getScr()?{ ??return?scr; ?} ?public?void?setScr(Set<StudentCourseRelation>?scr)?{ ??this.scr?=?scr; ?} }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 <?xml?version="1.0"?encoding="UTF-8"?> <!DOCTYPE?hibernate-mapping?PUBLIC??? ?"-//Hibernate/Hibernate?Mapping?DTD?3.0//EN"?? ?"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">? ?<hibernate-mapping> ??<class?name="com.study.hibernate.domain.Student"?table="t_student"?catalog="db_hibernate"> ???<id?name="id"?column="id"?type="java.lang.Long"> ????<generator??class="native"></generator> ???</id> ???? ???<property?name="name"?column="name"?type="java.lang.String"></property> ???? ???? ???<set?name="scr"?cascade="all"> ????<key?column="student_id"/> ????<one-to-many?class="com.study.hibernate.domain.StudentCourseRelation"/> ???</set> ??</class> ?</hibernate-mapping>

????課程類:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 /** ?*?課程 ?*/ public?class?Course?{ ?private?Long?id; ?private?String?name; ?? ?//與學生課程關聯表建立一對多關系 ?private?Set<StudentCourseRelation>?scr?=?new?HashSet<StudentCourseRelation>(); ?? ?public?Course(){ ??? ?} ?? ?public?Long?getId()?{ ??return?id; ?} ?public?void?setId(Long?id)?{ ??this.id?=?id; ?} ?public?String?getName()?{ ??return?name; ?} ?public?void?setName(String?name)?{ ??this.name?=?name; ?} ?public?Set<StudentCourseRelation>?getScr()?{ ??return?scr; ?} ?public?void?setScr(Set<StudentCourseRelation>?scr)?{ ??this.scr?=?scr; ?} }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <?xml?version="1.0"?encoding="UTF-8"?> <!DOCTYPE?hibernate-mapping?PUBLIC??? ?"-//Hibernate/Hibernate?Mapping?DTD?3.0//EN"?? ?"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">? ?<hibernate-mapping> ??<class?name="com.study.hibernate.domain.Course"?table="t_course"?catalog="db_hibernate"> ???<id?name="id"?column="id"?type="java.lang.Long"> ????<generator??class="native"></generator> ???</id> ???<property?name="name"?column="name"?type="java.lang.String"></property> ???<set?name="scr"?cascade="all"> ????<key?column="course_id"/> ????<one-to-many?class="com.study.hibernate.domain.StudentCourseRelation"/> ???</set> ??</class> ?</hibernate-mapping>

????學生課程關聯類:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 /** ?*?學生課程關聯表 ?*/ public?class?StudentCourseRelation?{ ?private?Long?id; ?private?Long?sort; ?private?Student?students; ?private?Course?courses; ?? ?public?Long?getId()?{ ??return?id; ?} ?public?void?setId(Long?id)?{ ??this.id?=?id; ?} ?? ?public?Long?getSort()?{ ??return?sort; ?} ?public?void?setSort(Long?sort)?{ ??this.sort?=?sort; ?} ?public?Student?getStudents()?{ ??return?students; ?} ?public?void?setStudents(Student?students)?{ ??this.students?=?students; ?} ?public?Course?getCourses()?{ ??return?courses; ?} ?public?void?setCourses(Course?courses)?{ ??this.courses?=?courses; ?} }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 <?xml?version="1.0"?encoding="UTF-8"?> <!DOCTYPE?hibernate-mapping?PUBLIC??? ?"-//Hibernate/Hibernate?Mapping?DTD?3.0//EN"?? ?"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">? ?<hibernate-mapping> ??<class?name="com.study.hibernate.domain.StudentCourseRelation"?table="t_student_course_relation"?catalog="db_hibernate"> ???<id?name="id"?column="id"?type="java.lang.Long"> ????<generator??class="native"></generator> ???</id> ???<property?name="sort"?column="sort"?type="java.lang.Long"></property> ???<many-to-one?name="students"?class="com.study.hibernate.domain.Student"?column="student_id"></many-to-one> ???<many-to-one?name="courses"?class="com.study.hibernate.domain.Course"?column="course_id"></many-to-one> ??</class> ?</hibernate-mapping>

????測試代碼:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 /** ??*?插入數據 ??*/ ?@Test ?public?void?insertTest(){ ??Configuration?configuration?=?new?Configuration().configure(); ??ServiceRegistry?serviceRegistry?=?new?ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry(); ??SessionFactory?sessionFactory?=?configuration.buildSessionFactory(serviceRegistry); ??Session?session?=?sessionFactory.openSession(); ??Transaction?transaction?=?session.beginTransaction(); ??? ??Student?stu1?=?new?Student(); ??stu1.setName("zhangsan"); ??? ??Course?c1?=?new?Course(); ??c1.setName("English"); ??? ??session.persist(stu1); ??session.persist(c1); ??? ??StudentCourseRelation?scr?=?new?StudentCourseRelation(); ??scr.setStudents(stu1); ??scr.setCourses(c1); ??session.persist(scr); ??transaction.commit(); ??session.close(); ??sessionFactory.close(); ??? ??//結果 //??Hibernate:?/*?insert?com.study.hibernate.domain.Student?*/?insert?into?db_hibernate.t_student?(name)?values?(?) //??Hibernate:?/*?insert?com.study.hibernate.domain.Course?*/?insert?into?db_hibernate.t_course?(name)?values?(?) //??Hibernate:?/*?insert?com.study.hibernate.domain.StudentCourseRelation?*/?insert?into?db_hibernate.t_student_course_relation?(student_id,?course_id)?values?(?,??) ?}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 ?/** ??*?查詢數據 ??*/ ?@Test ?public?void?queryTest(){ ??Configuration?configuration?=?new?Configuration().configure(); ??ServiceRegistry?serviceRegistry?=?new?ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry(); ??SessionFactory?sessionFactory?=?configuration.buildSessionFactory(serviceRegistry); ??Session?session?=?sessionFactory.openSession(); ??Transaction?transaction?=?session.beginTransaction(); ??? ??? ??Student?stu?=?(Student)?session.load(Student.class,?new?Long(2)); ??Set<StudentCourseRelation>?scr?=??stu.getScr(); ??for?(StudentCourseRelation?studentCourseRelation?:?scr)?{ ???System.out.println(studentCourseRelation.getCourses().getId()); ???System.out.println(studentCourseRelation.getCourses().getName()); ??} ??? ??? ??transaction.commit(); ??session.close(); ??sessionFactory.close(); ?}

????結果:

1 2 3 4 Hibernate:?/*?load?com.study.hibernate.domain.Student?*/?select?student0_.id?as?id1_1_0_,?student0_.name?as?name2_1_0_?from?db_hibernate.t_student?student0_?where?student0_.id=? Hibernate:?/*?load?one-to-many?com.study.hibernate.domain.Student.scr?*/?select?scr0_.student_id?as?student_3_1_1_,?scr0_.id?as?id1_2_1_,?scr0_.id?as?id1_2_0_,?scr0_.sort?as?sort2_2_0_,?scr0_.student_id?as?student_3_2_0_,?scr0_.course_id?as?course_i4_2_0_?from?db_hibernate.t_student_course_relation?scr0_?where?scr0_.student_id=? Hibernate:?/*?load?com.study.hibernate.domain.Course?*/?select?course0_.id?as?id1_0_0_,?course0_.name?as?name2_0_0_?from?db_hibernate.t_course?course0_?where?course0_.id=? 2:English

總結

以上是生活随笔為你收集整理的Hibernate从零开始_07_多对多关系(中间表)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。