您好,欢迎访问一九零五行业门户网

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-813 ]]>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_关联映射_一对多的详细内容。
其它类似信息

推荐信息