Hibernate中映射一对一关联(按主键映射和外键映射)和组件映射

                                                                     

                                                    Hibernate中映射一对一关联(按主键映射和外键映射)和组件映射

 

Hibernate提供了两种映射一对一关联关系的方式:按照外键映射按照主键映射

下面以员工账号员工档案表为例,介绍这两种映射方式,并使用这两种映射方式分别完成以下持久化操作:

(1)保存员工档案的同时分配给员工一个账号。

(2)加载员工档案的同时加载账号信息。

1.按照外键映射

步骤一:创建实体类Users1和Resume1

 

 1 package cn.happy.onetoone.fk;
 2 
 3 public class Resume1 {
 4     private Integer resId;
 5     private Integer resUserId;
 6     private String resName;
 7     private String resCardNo;
 8     private Users1 users1;
 9     
10     
11     public Integer getResId() {
12         return resId;
13     }
14     public void setResId(Integer resId) {
15         this.resId = resId;
16     }
17     public Integer getResUserId() {
18         return resUserId;
19     }
20     public void setResUserId(Integer resUserId) {
21         this.resUserId = resUserId;
22     }
23     public String getResName() {
24         return resName;
25     }
26     public void setResName(String resName) {
27         this.resName = resName;
28     }
29     public String getResCardNo() {
30         return resCardNo;
31     }
32     public void setResCardNo(String resCardNo) {
33         this.resCardNo = resCardNo;
34     }
35     public Users1 getUsers1() {
36         return users1;
37     }
38     public void setUsers1(Users1 users1) {
39         this.users1 = users1;
40     }
41     
42     
43 }
Resume1
 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC
 3         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4         "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 5         <hibernate-mapping package="cn.happy.onetoone.fk">
 6         
 7         <class name="Resume1" table="Resume1">
 8         <id name="resId" column="RESID">
 9          <generator class="sequence">
10          <param name="sequence">STU_SID</param>
11          </generator>
12         </id>
13         <property name="resName" type="string" column="RESRNAME"></property>
14         <property name="resCardNo" type="string" column="RESCARDNO"></property>
15         <many-to-one name="users1" class="Users1" column="RESUSERSID" cascade="all" unique="true"></many-to-one>
16         </class>
17         
18         </hibernate-mapping>
Resume1.hbm.xml

 

 1 package cn.happy.onetoone.fk;
 2 
 3 public class Users1 {
 4     private Integer userId;
 5     private String userName;
 6     private String userpass;
 7     public String getUserpass() {
 8         return userpass;
 9     }
10     public void setUserpass(String userpass) {
11         this.userpass = userpass;
12     }
13     private Resume1 resume1;
14     
15     
16     
17     public Integer getUserId() {
18         return userId;
19     }
20     public void setUserId(Integer userId) {
21         this.userId = userId;
22     }
23     public String getUserName() {
24         return userName;
25     }
26     public void setUserName(String userName) {
27         this.userName = userName;
28     }
29     
30     public Resume1 getResume1() {
31         return resume1;
32     }
33     public void setResume1(Resume1 resume1) {
34         this.resume1 = resume1;
35     }
36 }
Users1.java
 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC
 3         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4         "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 5         <hibernate-mapping package="cn.happy.onetoone.fk">
 6         
 7         <class name="Users1" table="Users1">
 8         <id name="userId" column="USERID">
 9          <generator class="sequence">
10          <param name="sequence">STU_SID</param>
11          </generator>
12         </id>
13         <property name="userName" type="string" column="USERNAME"></property>
14         <property name="userpass" type="string" column="USERPASS"></property>
15         <one-to-one name="resume1" class="Resume1" property-ref="users1"></one-to-one>
16         </class>
17         
18         </hibernate-mapping>
Users1.hbm.xml

步骤二:配置文件Users1.hbm.xml和Resume1.hbm.xml

步骤三:测试方法书写

 1 package cn.happy.onetoone.fk;
 2 
 3 import org.hibernate.Session;
 4 import org.hibernate.Transaction;
 5 import org.junit.Test;
 6 
 7 import cn.happy.util.HibernateUtil;
 8 
 9 public class Testone {
10     /**
11      * 一对一    按外键映射
12      * **/
13      @Test
14      public void onetoont(){
15          Session session = HibernateUtil.currentSession();
16          Transaction tx = session.beginTransaction();
17          Resume1 re=new Resume1();
18          re.setResName("学院不");
19          re.setResCardNo("002");
20          
21          Users1 use=new Users1();
22          use.setUserName("王喜");
23          
24          use.setResume1(re);
25          re.setUsers1(use);
26          
27          session.save(re);
28          tx.commit();
29          HibernateUtil.closeSession();
30          
31      }
32      
33      
34      @Test
35      public void select(){
36          Session session = HibernateUtil.currentSession();
37          Transaction tx = session.beginTransaction();
38          Users1 u =(Users1) session.get(Users1.class, 2);
39          System.out.println(u.getResume1().getResName());
40          tx.commit();
41          HibernateUtil.closeSession();
42      }
43      
44      
45  
46      
47 }
测试

 

二:按照主键映射

步骤一:创建实体类Users2和Resume2

 1 package cn.happy.onetoone.pk;
 2 
 3 public class Resume2 {
 4     private Integer resId;
 5     private Integer resUserId;
 6     private String resName;
 7     private String resCardNo;
 8     private Users2 users1;
 9     
10     
11     public Integer getResId() {
12         return resId;
13     }
14     public void setResId(Integer resId) {
15         this.resId = resId;
16     }
17     public Integer getResUserId() {
18         return resUserId;
19     }
20     public void setResUserId(Integer resUserId) {
21         this.resUserId = resUserId;
22     }
23     public String getResName() {
24         return resName;
25     }
26     public void setResName(String resName) {
27         this.resName = resName;
28     }
29     public String getResCardNo() {
30         return resCardNo;
31     }
32     public void setResCardNo(String resCardNo) {
33         this.resCardNo = resCardNo;
34     }
35     public Users2 getUsers1() {
36         return users1;
37     }
38     public void setUsers1(Users2 users1) {
39         this.users1 = users1;
40     }
41     
42     
43 }
Resume2.java
 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC
 3         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4         "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 5         <hibernate-mapping package="cn.happy.onetoone.pk">
 6         
 7         <class name="Resume2" table="Resume2">
 8         <id name="resId" column="RESID">
 9          <generator class="sequence">
10          <param name="sequence">STU_SID</param>
11          </generator>
12         </id>
13         <property name="resName" type="string" column="RESRNAME"></property>
14         <property name="resCardNo" type="string" column="RESCARDNO"></property>
15         <one-to-one name="users1" class="Users2" cascade="all" ></one-to-one>
16         </class>
17         
18         </hibernate-mapping>
Resume2.hbm.xml

 

 

 1 package cn.happy.onetoone.pk;
 2 
 3 public class Users2 {
 4     private Integer userId;
 5     private String userName;
 6     private Resume2 resume1;
 7     
 8     
 9     
10     public Integer getUserId() {
11         return userId;
12     }
13     public void setUserId(Integer userId) {
14         this.userId = userId;
15     }
16     public String getUserName() {
17         return userName;
18     }
19     public void setUserName(String userName) {
20         this.userName = userName;
21     }
22     
23     public Resume2 getResume1() {
24         return resume1;
25     }
26     public void setResume1(Resume2 resume1) {
27         this.resume1 = resume1;
28     }
29 }
Users2.java
 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC
 3         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4         "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 5         <hibernate-mapping package="cn.happy.onetoone.pk">
 6         
 7         <class name="Users2" table="Users2">
 8         <id name="userId" column="USERID">
 9          <generator class="foreign">
10          <param name="property">resume1</param>
11          </generator>
12         </id>
13         <property name="userName" type="string" column="USERNAME"></property>
14         
15         <one-to-one name="resume1" class="Resume2" constrained="true"></one-to-one>
16         </class>
17         
18         </hibernate-mapping>
Users2.hbm.xml

步骤二:配置文件Users2.hbm.xml和Resume2.hbm.xml

 

 

 

 

 步骤三:测试方法书写

 1 package cn.happy.onetoone.pk;
 2 
 3 import org.hibernate.Session;
 4 import org.hibernate.Transaction;
 5 import org.junit.Test;
 6 
 7 import cn.happy.util.HibernateUtil;
 8 
 9 public class Testone {
10     /**
11      * 一对一  按主键映射
12      * */
13      @Test
14      public void onetoont(){
15          Session session = HibernateUtil.currentSession();
16          Transaction tx = session.beginTransaction();
17          Resume2 re=new Resume2();
18          re.setResName("学院不2");
19          re.setResCardNo("2");
20          
21          Users2 use=new Users2();
22          use.setUserName("王喜2");
23          
24          use.setResume1(re);
25          re.setUsers1(use);
26          
27          session.save(re);
28          tx.commit();
29          HibernateUtil.closeSession();
30          
31      }
32      
33      
34      @Test
35      public void select(){
36          Session session = HibernateUtil.currentSession();
37          Transaction tx = session.beginTransaction();
38          Users2 u =(Users2) session.get(Users2.class, 1);
39          System.out.println(u.getResume1().getResName());
40          tx.commit();
41          HibernateUtil.closeSession();
42      }
43      
44      
45      
46 }
测试代码

 

 

 

 3.组件映射

建立关系数据模型的一个重要原则是在不会导致数据冗余的前提下,尽可能减少数据库表的数目及表之间的外键参照关系。以员工信息为例,员工信息中有员工的家庭地址信息,如果把地址信息单独放在一张表中,然后建立员工信息表和地址信息表之间的外键参照关系,当每次查询员工信息时,都需建立者两个表的连接。建立表的连接是很耗时的操作,为了提高数据库运行性能,可以把这两张表的信息整合在一张员工信息表EMPINFO中

 

 

步骤一:创建EmpHomeAddress和EmpInfo

 1 package cn.happy.zujian;
 2 
 3 public class EmpInfo {
 4   private Integer eId;
 5   private String eName;
 6   private EmpHomeAddress eHome;
 7   
 8   
 9 public Integer geteId() {
10     return eId;
11 }
12 public void seteId(Integer eId) {
13     this.eId = eId;
14 }
15 public String geteName() {
16     return eName;
17 }
18 public void seteName(String eName) {
19     this.eName = eName;
20 }
21 public EmpHomeAddress geteHome() {
22     return eHome;
23 }
24 public void seteHome(EmpHomeAddress eHome) {
25     this.eHome = eHome;
26 }
27 }
EmpInfo.java
 1 package cn.happy.zujian;
 2 
 3 public class EmpHomeAddress {
 4   private String ehomestreet;
 5   private String ehomecity;
 6   private String ehomeprovince;
 7   private String ehomezipcode;
 8   
 9   private EmpInfo empinfo;
10 public EmpInfo getEmpinfo() {
11     return empinfo;
12 }
13 public void setEmpinfo(EmpInfo empinfo) {
14     this.empinfo = empinfo;
15 }
16 public String getEhomestreet() {
17     return ehomestreet;
18 }
19 public void setEhomestreet(String ehomestreet) {
20     this.ehomestreet = ehomestreet;
21 }
22 public String getEhomecity() {
23     return ehomecity;
24 }
25 public void setEhomecity(String ehomecity) {
26     this.ehomecity = ehomecity;
27 }
28 public String getEhomeprovince() {
29     return ehomeprovince;
30 }
31 public void setEhomeprovince(String ehomeprovince) {
32     this.ehomeprovince = ehomeprovince;
33 }
34 public String getEhomezipcode() {
35     return ehomezipcode;
36 }
37 public void setEhomezipcode(String ehomezipcode) {
38     this.ehomezipcode = ehomezipcode;
39 }
40   
41 }
EmpHomeAddress
 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC
 3         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4         "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 5         <hibernate-mapping package="cn.happy.zujian">
 6         
 7         <class name="EmpInfo" table="EMPINFO">
 8         <id name="eId" column="EID">
 9          <generator class="sequence">
10          <param name="sequence">STU_SID</param>
11          </generator>
12         </id>
13         <property name="eName" type="string" column="ENAME"></property>
14         
15         <component name="eHome" class="EmpHomeAddress">
16         <parent name="empinfo"/>
17         <property name="ehomestreet" column="EHOMESTREET" type="string"></property>
18         <property name="ehomecity" column="EHOMECITY" type="string"></property>
19         <property name="ehomeprovince" column="EHOMEPROVINCE" type="string"></property>
20         <property name="ehomezipcode" column="EHOMEZIPCODE" type="string"></property>
21         </component>
22        
23        
24         </class>
25         
26         </hibernate-mapping>
EmpInfo.hbm.xml

 

步骤二:创建配置文件EmpInfo.hbm.xml

 

步骤三:书写测试类

 1 package cn.happy.zujian;
 2 
 3 import org.hibernate.Session;
 4 import org.hibernate.Transaction;
 5 import org.junit.Test;
 6 
 7 import cn.happy.util.HibernateUtil;
 8 
 9 public class TestInfo {
10     /**
11      * 组件映射 
12      * */
13   @Test
14   public void zujian(){
15       Session session = HibernateUtil.currentSession();
16       Transaction tx = session.beginTransaction();
17       
18       EmpInfo empinfo=new EmpInfo();
19       empinfo.seteName("回青");
20       
21       EmpHomeAddress address=new EmpHomeAddress();
22       address.setEhomecity("北京");
23       address.setEhomeprovince("北京市");
24       address.setEhomestreet("成府路");
25       address.setEhomezipcode("10002");
26       empinfo.seteHome(address);
27       
28       session.save(empinfo);
29       tx.commit();
30       
31       HibernateUtil.closeSession();
32       
33   }
34 }
测试代码

 

posted @ 2016-08-18 15:19  回青  阅读(5978)  评论(0编辑  收藏  举报