码迷,mamicode.com
首页 > Web开发 > 详细

Hibernate中多对多

时间:2018-06-12 13:41:04      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:.com   this   height   spl   size   image   ext   ima   分享   

 

学生 - 老师

技术分享图片
 1 package com.zhangpn.entity;
 2 
 3 import java.util.HashSet;
 4 import java.util.Set;
 5 
 6 public class Student {
 7     private Long id;
 8     private String name;
 9     private Set<Teacher> teachers = new HashSet<>();
10 
11     public Long getId() {
12         return id;
13     }
14 
15     public void setId(Long id) {
16         this.id = id;
17     }
18 
19     public String getName() {
20         return name;
21     }
22 
23     public void setName(String name) {
24         this.name = name;
25     }
26 
27     public Set<Teacher> getTeachers() {
28         return teachers;
29     }
30 
31     public void setTeachers(Set<Teacher> teachers) {
32         this.teachers = teachers;
33     }
34 
35     public Student() {
36     }
37 
38     public Student(String name, Set<Teacher> teachers) {
39         this.name = name;
40         this.teachers = teachers;
41     }
42 
43 }
Student
技术分享图片
 1 package com.zhangpn.entity;
 2 
 3 import java.util.HashSet;
 4 import java.util.Set;
 5 
 6 public class Teacher {
 7     private Long id;
 8     private String name;
 9     private Set<Student> students = new HashSet<>();
10 
11     public Long getId() {
12         return id;
13     }
14 
15     public void setId(Long id) {
16         this.id = id;
17     }
18 
19     public String getName() {
20         return name;
21     }
22 
23     public void setName(String name) {
24         this.name = name;
25     }
26 
27     public Set<Student> getStudents() {
28         return students;
29     }
30 
31     public void setStudents(Set<Student> students) {
32         this.students = students;
33     }
34 
35     public Teacher() {
36     }
37 
38     public Teacher(String name, Set<Student> students) {
39         this.name = name;
40         this.students = students;
41     }
42 
43 }
Teacher

学生配置 - 老师配置

技术分享图片
 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 4 <hibernate-mapping>
 5     <class name="com.zhangpn.entity.Student" table="STUDENT">
 6         <id name="id" type="java.lang.Long">
 7             <column name="ID" />
 8             <generator class="native" />
 9         </id>
10         <property name="name" type="java.lang.String">
11             <column name="NAME" />
12         </property>
13         <set name="teachers" table="STUDENT_TEACHER" inverse="false" lazy="true">
14             <key>
15                 <column name="STUDENTID" />
16             </key>
17             <many-to-many class="com.zhangpn.entity.Teacher" column="TEACHERID"></many-to-many>
18         </set>
19     </class>
20 </hibernate-mapping>
Student.hbm.xml
技术分享图片
 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 4 <hibernate-mapping>
 5     <class name="com.zhangpn.entity.Teacher" table="TEACHER">
 6         <id name="id" type="java.lang.Long">
 7             <column name="ID" />
 8             <generator class="native" />
 9         </id>
10         <property name="name" type="java.lang.String">
11             <column name="NAME" />
12         </property>
13         <set name="students" table="STUDENT_TEACHER" inverse="false" lazy="true">
14             <key>
15                 <column name="TEACHERID" />
16             </key>
17             <many-to-many class="com.zhangpn.entity.Student" column="STUDENTID"></many-to-many>
18         </set>
19     </class>
20 </hibernate-mapping>
Teacher.hbm.xml

中间表

学生
id name
   
中间表
stu_id tea_id
   
老师
id name
   

 

 

 


 学生表

技术分享图片

主键:ID


教师表

技术分享图片

主键:ID


 

中间表: 

技术分享图片

技术分享图片

主键:STUDENTID、TEACHERID

外键:STUDENTID、TEACHERID

Hibernate中多对多

标签:.com   this   height   spl   size   image   ext   ima   分享   

原文地址:https://www.cnblogs.com/batj/p/9172503.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!