Hibernate的关系映射

来源:互联网 发布:shopnc 源码下载 编辑:程序博客网 时间:2024/06/11 09:58

Dept

[java] view plain copy
  1. package cn.et.hibernate.lesson02.relation;  
  2.   
  3. import java.util.HashSet;  
  4. import java.util.Set;  
  5.   
  6. /** 
  7.  * Dept entity. @author MyEclipse Persistence Tools 
  8.  */  
  9.   
  10. public class Dept implements java.io.Serializable {  
  11.   
  12.     // Fields  
  13.   
  14.     private Short deptno;  
  15.     private String dname;  
  16.     private String loc;  
  17.     private Set emps = new HashSet(0);  
  18.   
  19.     // Constructors  
  20.   
  21.     /** default constructor */  
  22.     public Dept() {  
  23.     }  
  24.   
  25.     /** minimal constructor */  
  26.     public Dept(Short deptno) {  
  27.         this.deptno = deptno;  
  28.     }  
  29.   
  30.     /** full constructor */  
  31.     public Dept(Short deptno, String dname, String loc, Set emps) {  
  32.         this.deptno = deptno;  
  33.         this.dname = dname;  
  34.         this.loc = loc;  
  35.         this.emps = emps;  
  36.     }  
  37.   
  38.     // Property accessors  
  39.   
  40.     public Short getDeptno() {  
  41.         return this.deptno;  
  42.     }  
  43.   
  44.     public void setDeptno(Short deptno) {  
  45.         this.deptno = deptno;  
  46.     }  
  47.   
  48.     public String getDname() {  
  49.         return this.dname;  
  50.     }  
  51.   
  52.     public void setDname(String dname) {  
  53.         this.dname = dname;  
  54.     }  
  55.   
  56.     public String getLoc() {  
  57.         return this.loc;  
  58.     }  
  59.   
  60.     public void setLoc(String loc) {  
  61.         this.loc = loc;  
  62.     }  
  63.   
  64.     public Set getEmps() {  
  65.         return this.emps;  
  66.     }  
  67.   
  68.     public void setEmps(Set emps) {  
  69.         this.emps = emps;  
  70.     }  
  71.   
  72. }  


Emp

[java] view plain copy
  1. package cn.et.hibernate.lesson02.relation;  
  2.   
  3. import java.util.Date;  
  4.   
  5. /** 
  6.  * Emp entity. @author MyEclipse Persistence Tools 
  7.  */  
  8.   
  9. public class Emp implements java.io.Serializable {  
  10.   
  11.     // Fields  
  12.   
  13.     private Short empno;  
  14.     private Dept dept;  
  15.     private String ename;  
  16.     private String job;  
  17.     private Short mgr;  
  18.     private Date hiredate;  
  19.     private Double sal;  
  20.     private Double comm;  
  21.     private String sex;  
  22.   
  23.     // Constructors  
  24.   
  25.     /** default constructor */  
  26.     public Emp() {  
  27.     }  
  28.   
  29.     /** minimal constructor */  
  30.     public Emp(Short empno) {  
  31.         this.empno = empno;  
  32.     }  
  33.   
  34.     /** full constructor */  
  35.     public Emp(Short empno, Dept dept, String ename, String job, Short mgr,  
  36.             Date hiredate, Double sal, Double comm, String sex) {  
  37.         this.empno = empno;  
  38.         this.dept = dept;  
  39.         this.ename = ename;  
  40.         this.job = job;  
  41.         this.mgr = mgr;  
  42.         this.hiredate = hiredate;  
  43.         this.sal = sal;  
  44.         this.comm = comm;  
  45.         this.sex = sex;  
  46.     }  
  47.   
  48.     // Property accessors  
  49.   
  50.     public Short getEmpno() {  
  51.         return this.empno;  
  52.     }  
  53.   
  54.     public void setEmpno(Short empno) {  
  55.         this.empno = empno;  
  56.     }  
  57.   
  58.     public Dept getDept() {  
  59.         return this.dept;  
  60.     }  
  61.   
  62.     public void setDept(Dept dept) {  
  63.         this.dept = dept;  
  64.     }  
  65.   
  66.     public String getEname() {  
  67.         return this.ename;  
  68.     }  
  69.   
  70.     public void setEname(String ename) {  
  71.         this.ename = ename;  
  72.     }  
  73.   
  74.     public String getJob() {  
  75.         return this.job;  
  76.     }  
  77.   
  78.     public void setJob(String job) {  
  79.         this.job = job;  
  80.     }  
  81.   
  82.     public Short getMgr() {  
  83.         return this.mgr;  
  84.     }  
  85.   
  86.     public void setMgr(Short mgr) {  
  87.         this.mgr = mgr;  
  88.     }  
  89.   
  90.     public Date getHiredate() {  
  91.         return this.hiredate;  
  92.     }  
  93.   
  94.     public void setHiredate(Date hiredate) {  
  95.         this.hiredate = hiredate;  
  96.     }  
  97.   
  98.     public Double getSal() {  
  99.         return this.sal;  
  100.     }  
  101.   
  102.     public void setSal(Double sal) {  
  103.         this.sal = sal;  
  104.     }  
  105.   
  106.     public Double getComm() {  
  107.         return this.comm;  
  108.     }  
  109.   
  110.     public void setComm(Double comm) {  
  111.         this.comm = comm;  
  112.     }  
  113.   
  114.     public String getSex() {  
  115.         return this.sex;  
  116.     }  
  117.   
  118.     public void setSex(String sex) {  
  119.         this.sex = sex;  
  120.     }  
  121.   
  122. }  

Dept.hbm.xml

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  4. <!--  
  5.     Mapping file autogenerated by MyEclipse Persistence Tools 
  6. -->  
  7. <hibernate-mapping>  
  8.     <class name="cn.et.hibernate.lesson02.relation.Dept" table="DEPT" schema="SCOTT">  
  9.         <id name="deptno" type="java.lang.Short">  
  10.             <column name="DEPTNO" precision="3" scale="0" />  
  11.             <generator class="assigned" />  
  12.         </id>  
  13.         <property name="dname" type="java.lang.String">  
  14.             <column name="DNAME" length="14">  
  15.                 <comment>部门名称</comment>  
  16.             </column>  
  17.         </property>  
  18.         <property name="loc" type="java.lang.String">  
  19.             <column name="LOC" length="13">  
  20.                 <comment>部门所在位置</comment>  
  21.             </column>  
  22.         </property>  
  23.           
  24.           
  25.         <set name="emps" inverse="true">  
  26.             <key>  
  27.                 <column name="DEPTNO">  
  28.                     <comment>所属部门编号</comment>  
  29.                 </column>  
  30.             </key>  
  31.             <one-to-many class="cn.et.hibernate.lesson02.relation.Emp" />  
  32.         </set>  
  33.           
  34.     </class>  
  35. </hibernate-mapping>  


Emp.hbm.xml

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  4. <!--  
  5.     Mapping file autogenerated by MyEclipse Persistence Tools 
  6. -->  
  7. <hibernate-mapping>  
  8.     <class name="cn.et.hibernate.lesson02.relation.Emp" table="EMP" schema="SCOTT">  
  9.         <id name="empno" type="java.lang.Short">  
  10.             <column name="EMPNO" precision="4" scale="0" />  
  11.             <generator class="assigned" />  
  12.         </id>  
  13.           
  14.           
  15.         <many-to-one name="dept" class="cn.et.hibernate.lesson02.relation.Dept" fetch="select">  
  16.             <column name="DEPTNO" precision="2" scale="0">  
  17.                 <comment>所属部门编号</comment>  
  18.             </column>  
  19.         </many-to-one>  
  20.           
  21.           
  22.         <property name="ename" type="java.lang.String">  
  23.             <column name="ENAME" length="10">  
  24.                 <comment>员工姓名</comment>  
  25.             </column>  
  26.         </property>  
  27.         <property name="job" type="java.lang.String">  
  28.             <column name="JOB" length="9">  
  29.                 <comment>职位</comment>  
  30.             </column>  
  31.         </property>  
  32.         <property name="mgr" type="java.lang.Short">  
  33.             <column name="MGR" precision="4" scale="0">  
  34.                 <comment>领导编号</comment>  
  35.             </column>  
  36.         </property>  
  37.         <property name="hiredate" type="java.util.Date">  
  38.             <column name="HIREDATE" length="7">  
  39.                 <comment>雇佣日期</comment>  
  40.             </column>  
  41.         </property>  
  42.         <property name="sal" type="java.lang.Double">  
  43.             <column name="SAL" precision="7">  
  44.                 <comment>月薪</comment>  
  45.             </column>  
  46.         </property>  
  47.         <property name="comm" type="java.lang.Double">  
  48.             <column name="COMM" precision="7">  
  49.                 <comment>奖金</comment>  
  50.             </column>  
  51.         </property>  
  52.         <property name="sex" type="java.lang.String">  
  53.             <column name="SEX" length="3" />  
  54.         </property>  
  55.     </class>  
  56. </hibernate-mapping>  

hibernate.cfg.xml

[html] view plain copy
  1. <?xml version='1.0' encoding='UTF-8'?>  
  2. <!DOCTYPE hibernate-configuration PUBLIC  
  3.           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  4.           "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
  5.   
  6. <!-- Generated by MyEclipse Hibernate Tools. -->  
  7. <hibernate-configuration>  
  8.   
  9.     <session-factory>  
  10.         <!-- 告诉hibernate使用的是orcle数据库 -->  
  11.         <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>  
  12.         <!-- 配置session Factory四要素 -->  
  13.         <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>  
  14.         <property name="connection.username">scott</property>  
  15.         <property name="connection.password">tiger</property>  
  16.         <property name="connection.driver_class">oracle.jdbc.OracleDriver</property>  
  17.          
  18.         <!-- 打印日志 -->  
  19.         <property name="show_sql">true</property>  
  20.         <!-- 扫描映射文件 -->  
  21.         <mapping resource="cn/et/hibernate/lesson02/relation/Dept.hbm.xml"/>  
  22.         <mapping resource="cn/et/hibernate/lesson02/relation/Emp.hbm.xml"/>  
  23.     </session-factory>  
  24.   
  25. </hibernate-configuration>  

TestHibernate

[java] view plain copy
  1. package cn.et.hibernate.lesson02.relation;  
  2.   
  3. import org.hibernate.Session;  
  4. import org.hibernate.SessionFactory;  
  5. import org.hibernate.Transaction;  
  6. import org.hibernate.cfg.Configuration;  
  7. import org.junit.Test;  
  8.   
  9.   
  10. public class TestHibernate {  
  11.   
  12.     /** 
  13.      */  
  14.     @Test  
  15.     public void test(){  
  16.         SessionFactory sf = new Configuration().configure("/cn/et/hibernate/lesson02/relation/hibernate.cfg.xml").buildSessionFactory();  
  17.         Session session = sf.openSession();  
  18.           
  19.         Dept dept = (Dept) session.get(Dept.class,Short.parseShort("10"));  
  20.           
  21.         Emp emp = (Emp)dept.getEmps().iterator().next();  
  22.         System.out.println(emp.getEname());  
  23.     }  
  24.   
  25.       
  26.       
  27. }