hibernate_关联映射_一对多

Posted tzzt01

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hibernate_关联映射_一对多相关的知识,希望对你有一定的参考价值。

hibernate的映射关系

一对多、多对一、一对一、多对多。

常用的是一对多和多对一。

在数据库中可以通过添加主外键的关联,表现一对多的关系;在hibernate中通过在一方持有多方的集合实现,即在“一”的一端中使用<set>元素表示持有“多”的一端对象。

下面实现一个增删改查的“一对多”demo:一个班级对应多个学生。

首先创建学生类Student

技术分享
 1 package com.imooc.entity;
 2 
 3 import java.io.Serializable;
 4 
 5 public class Student implements Serializable {
 6 
 7     private int sid;
 8     private String sname;
 9     private String sex;
10     // 在多方定义一个一方的引用
11     private Grade grade;
12     
13     public int getSid() {
14         return sid;
15     }
16     public void setSid(int sid) {
17         this.sid = sid;
18     }
19     public String getSname() {
20         return sname;
21     }
22     public void setSname(String sname) {
23         this.sname = sname;
24     }
25     public String getSex() {
26         return sex;
27     }
28     public void setSex(String sex) {
29         this.sex = sex;
30     }
31     public Grade getGrade() {
32         return grade;
33     }
34     public void setGrade(Grade grade) {
35         this.grade = grade;
36     }
37     
38     public Student() {
39         super();
40     }
41     
42     public Student(String sname, String sex) {
43         super();
44         this.sname = sname;
45         this.sex = sex;
46     }
47     
48 }
View Code

创建班级类Grade

技术分享
 1 package com.imooc.entity;
 2 
 3 import java.io.Serializable;
 4 import java.util.HashSet;
 5 import java.util.Set;
 6 
 7 
 8 public class Grade implements Serializable {
 9 
10     private int gid;
11     private String gname;
12     private String gdesc;
13     private Set<Student> students = new HashSet<Student>();
14     
15     public int getGid() {
16         return gid;
17     }
18     public void setGid(int gid) {
19         this.gid = gid;
20     }
21     public String getGname() {
22         return gname;
23     }
24     public void setGname(String gname) {
25         this.gname = gname;
26     }
27     public String getGdesc() {
28         return gdesc;
29     }
30     public void setGdesc(String gdesc) {
31         this.gdesc = gdesc;
32     }
33     public Set<Student> getStudents() {
34         return students;
35     }
36     public void setStudents(Set<Student> students) {
37         this.students = students;
38     }
39     
40     public Grade() {
41         super();
42     }
43     
44     public Grade(int gid, String gname, String gdesc) {
45         super();
46         this.gid = gid;
47         this.gname = gname;
48         this.gdesc = gdesc;
49     }
50     
51     public Grade(String gname, String gdesc) {
52         super();
53         this.gname = gname;
54         this.gdesc = gdesc;
55     }
56 }
View Code

创建Student类的映射文件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 <!-- Generated 2017-6-1 14:49:09 by Hibernate Tools 3.5.0.Final -->
 5 <hibernate-mapping>
 6     <class name="com.imooc.entity.Student" table="STUDENT">
 7         <id name="sid" type="int">
 8             <column name="SID" />
 9             <generator class="increment" />
10         </id>
11         <property name="sname" type="java.lang.String">
12             <column name="SNAME" />
13         </property>
14         <property name="sex" type="java.lang.String">
15             <column name="SEX" />
16         </property>
17     </class>
18 </hibernate-mapping>
View Code

创建Grade类的映射文件Grade.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 <!-- Generated 2017-6-1 14:49:09 by Hibernate Tools 3.5.0.Final -->
 5 <hibernate-mapping>
 6     <class name="com.imooc.entity.Grade" table="GRADE">
 7         <id name="gid" type="int">
 8             <column name="GID" />
 9             <generator class="increment" />
10         </id>
11         <property name="gname" type="java.lang.String">
12             <column name="GNAME" length="20" not-null="true" />
13         </property>
14         <property name="gdesc" type="java.lang.String">
15             <column name="GDESC" />
16         </property>
17         <!-- 指定关联的外键列 -->
18         <set name="students" table="STUDENT">
19             <key>
20                 <column name="GID" />
21             </key>
22             <one-to-many class="com.imooc.entity.Student" />
23         </set>
24     </class>
25 </hibernate-mapping>
View Code

创建hibernate的配置文件

技术分享
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE hibernate-configuration PUBLIC
 3         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 4         "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
 5 <hibernate-configuration>
 6     <session-factory>
 7         <property name="connection.username">root</property>
 8         <property name="connection.password">root</property>
 9         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
10         <property name="connection.url">
11             <![CDATA[
12                 jdbc:mysql://localhost:3306/hibernate?useUnicode=true&amp;characterEncoding=UTF-8
13             ]]>
14         </property>
15         <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
16         <property name="show_sql">true</property>
17         <property name="format_sql">true</property>
18         <property name="hbm2ddl.auto">update</property>
19         
20         <!-- 指定映射文件的路径 -->
21         <mapping resource="com/imooc/entity/Grade.hbm.xml" />
22         <mapping resource="com/imooc/entity/Student.hbm.xml" />
23     </session-factory>
24 </hibernate-configuration>
View Code

写一个增删改查的测试文件

技术分享
 1 package com.imooc.test;
 2 
 3 import java.util.Set;
 4 
 5 import org.hibernate.Session;
 6 import org.hibernate.Transaction;
 7 
 8 import com.imooc.entity.Grade;
 9 import com.imooc.entity.Student;
10 import com.imooc.util.HibernateUtil;
11 
12 /*
13  * 单向一对多关系关系(班级--->学生)
14  * 建立关联关系后,可以方便的从一个对象导航到另一个对象
15  * 注意关联的方向
16  */
17 public class Test01 {
18 
19     public static void main(String[] args) {
20         //add();
21         //findStudentsByGrade();
22         //update();
23         delete();
24     }
25     
26     //将学生添加到班级
27     public static void add() {
28         Grade g = new Grade("Java一班", "Java软件开发一班");
29         Student s1 = new Student("杨康", "男");
30         Student s2 = new Student("穆念慈", "女");
31         
32         //如果希望在学生表中添加对应的班级编号,需要在班级中添加学生,建立关联关系
33         g.getStudents().add(s1);
34         g.getStudents().add(s2);
35         
36         Session session = HibernateUtil.getSession();
37         Transaction tr = session.beginTransaction();
38         session.save(g);
39         session.save(s1);
40         session.save(s2);
41         tr.commit();
42         HibernateUtil.closeSession(session);
43     }
44     
45     //查询班级中包含的学生
46     public static void findStudentsByGrade() {
47         Session session = HibernateUtil.getSession();
48         Grade grade = (Grade) session.get(Grade.class, 1);
49         System.out.println( grade.getGname() + "," + grade.getGdesc() );
50         
51         Set<Student> students = grade.getStudents();
52         for(Student s : students) {
53             System.out.println( s.getSname() + "," + s.getSex() );
54         }
55     }
56     
57     //修改学生信息
58     public static void update() {
59         Grade g=new Grade("Java二班", "Java软件开发二班");
60         Session session = HibernateUtil.getSession();
61         Transaction tr = session.beginTransaction();
62         Student s = (Student) session.get(Student.class, 1);
63         g.getStudents().add(s);
64         session.save(g);
65         tr.commit();
66         HibernateUtil.closeSession(session);
67     }
68     
69     //删除学生信息
70     public static void delete() {
71         Session session = HibernateUtil.getSession();
72         Transaction tr = session.beginTransaction();
73         Student s = (Student) session.get(Student.class, 2);
74         session.delete(s);
75         tr.commit();
76         HibernateUtil.closeSession(session);
77     }
78 }
View Code

以上是关于hibernate_关联映射_一对多的主要内容,如果未能解决你的问题,请参考以下文章

dljd_(057-058)_hibernate_关联关系映射-什么是关联关系

关联映射级联操作关系维护 ---- Hibernate之一对多|多对一关系

Hibernate一对多单向(双向)关联映射

hibernate一对多关联映射

学习笔记之Hibernate_映射关系之一对多映射关系

(转)Hibernate框架基础——一对多关联关系映射