hibernate_关联映射_多对一
Posted tzzt01
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hibernate_关联映射_多对一相关的知识,希望对你有一定的参考价值。
多对一关联关系和上一篇讲的一对多关联关系的不同点主要体现在映射文件上。
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 }
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 }
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 <!-- 配置多对一关联关系 --> 18 <many-to-one name="grade" class="com.imooc.entity.Grade" column="gid" cascade="all"></many-to-one> 19 </class> 20 </hibernate-mapping>
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 </class> 18 </hibernate-mapping>
hibernate.cfg.xml文件保持不变
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&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>
Test测试类:
1 package com.imooc.test; 2 3 import org.hibernate.Session; 4 import org.hibernate.Transaction; 5 6 import com.imooc.entity.Grade; 7 import com.imooc.entity.Student; 8 import com.imooc.util.HibernateUtil; 9 10 /* 11 * 单向多对一(学生--->班级) 12 */ 13 public class Test { 14 15 public static void main(String[] args) { 16 save(); 17 } 18 19 public static void save() { 20 Grade g = new Grade("java一班", "Java软件开发一班"); 21 Student s1 = new Student("杨过","男"); 22 Student s2 = new Student("小龙女", "女"); 23 24 // 设置关联关系 25 g.getStudents().add(s1); 26 g.getStudents().add(s2); 27 s1.setGrade(g); 28 s2.setGrade(g); 29 30 Session session = HibernateUtil.getSession(); 31 Transaction tr = session.beginTransaction(); 32 session.save(g); 33 session.save(s1); 34 session.save(s2); 35 tr.commit(); 36 HibernateUtil.closeSession(session); 37 } 38 }
以上是关于hibernate_关联映射_多对一的主要内容,如果未能解决你的问题,请参考以下文章