hibernate 1 对1

举例:部门departments   -------部门经理managers

映射 有两种方式

  1:外键映射。类似于多对1.但是设置了unique唯一。

  

带外键的:

 1 package com.hibernate.o2o;
 2 
 3 public class Departments {
 4     private Integer deptId;
 5     private String deptName;
 6     private Managers managers;
 7     public Integer getDeptId() {
 8         return deptId;
 9     }
10     public void setDeptId(Integer deptId) {
11         this.deptId = deptId;
12     }
13     public String getDeptName() {
14         return deptName;
15     }
16     public void setDeptName(String deptName) {
17         this.deptName = deptName;
18     }
19     public Managers getManagers() {
20         return managers;
21     }
22     public void setManagers(Managers managers) {
23         this.managers = managers;
24     }
25     
26 }
 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 
 5 <hibernate-mapping package="com.hibernate.o2o">
 6 
 7     <class name="Departments" table="departments">
 8         
 9         <id name="deptId" type="java.lang.Integer">
10             <column name="dept_Id" />
11             <!-- 指定主键的生成方式, native: 使用数据库本地方式 -->
12             <generator class="native" />
13         </id>
14     
15         <property name="deptName" 
16             type="java.lang.String" column="deptName" >
17         </property>
18         //就把它当成自己的一个属性。
19         <many-to-one name="managers" class="Managers" column="manager_Id"></many-to-one>
20         
21     </class>
22     
23 </hibernate-mapping>
 1 package com.hibernate.o2o;
 2 
 3 public class Managers {
 4     private Integer managerId;
 5     private String managerName;
 6     private Departments departments;
 7     public Integer getManagerId() {
 8         return managerId;
 9     }
10     public void setManagerId(Integer managerId) {
11         this.managerId = managerId;
12     }
13     public String getManagerName() {
14         return managerName;
15     }
16     public void setManagerName(String managerName) {
17         this.managerName = managerName;
18     }
19     public Departments getDepartments() {
20         return departments;
21     }
22     public void setDepartments(Departments departments) {
23         this.departments = departments;
24     }
25     
26 }
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.hibernate.o2o">

    <class name="Managers" table="managers" >
        
        <id name="managerId" type="java.lang.Integer">
            <column name="manager_Id" />
            <!-- 指定主键的生成方式, native: 使用数据库本地方式 -->
            <generator class="native" />
        </id>
    
        <property name="managerName"
            type="java.lang.String" column="manager_Name" >
        </property>
        
        <!-- 1-1的关联关系 property-ref="managers"  这句话的意思就是把部门表的经理列对应的数据库中表的字段作为连接条件。-->
        <one-to-one name="departments" class="Departments"  property-ref="managers"></one-to-one>
    </class>
    
</hibernate-mapping>

查询:

 1 @Test
 2     public void testGet(){
 3         //1. 默认情况下对关联属性使用懒加载
 4         Department dept = (Department) session.get(Department.class, 1);
 5         System.out.println(dept.getDeptName()); 
 6         
 7         //2. 所以会出现懒加载异常的问题. 
 8 //        session.close();
 9 //        Manager mgr = dept.getMgr();
10 //        System.out.println(mgr.getClass()); 
11 //        System.out.println(mgr.getMgrName()); 
12         
13         //3. 查询 Manager 对象的连接条件应该是 dept.manager_id = mgr.manager_id
14         //而不应该是 dept.dept_id = mgr.manager_id
15         Manager mgr = dept.getMgr();
16         System.out.println(mgr.getMgrName()); 
17         
18         
19     }

查询2:

@Test
    public void testGet2(){
        //在查询没有外键的实体对象时, 使用的左外连接查询, 一并查询出其关联的对象
        //并已经进行初始化. 
        Manager mgr = (Manager) session.get(Manager.class, 1);
        System.out.println(mgr.getMgrName()); 
        System.out.println(mgr.getDept().getDeptName()); 
    }

利用主键生成关联映射  

   需要依赖另一个表的主键来生成自己的主键。

 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 
 5 <hibernate-mapping package="com.hibernate.o2o">
 6 
 7     <class name="Departments" table="departments">
 8         
 9         <id name="deptId" type="java.lang.Integer">
10             <column name="dept_Id" />
11                 <!-- 使用外键的方式来生成当前的主键 -->
12                 <!-- property 属性指定使用当前持久化类的哪一个属性的主键作为外键 -->
13             <generator class="foreign" >
14                 <param name="property">managers</param>
15             </generator>
16         </id>
17     
18         <property name="deptName" 
19             type="java.lang.String" column="deptName" >
20         </property>
21         <!--  
22         采用 foreign 主键生成器策略的一端增加 one-to-one 元素映射关联属性,
23         其 one-to-one 节点还应增加 constrained=true 属性, 以使当前的主键上添加外键约束
24         -->
25         <one-to-one name="managers" class="Managers" constrained="true"></one-to-one>
26         
27     </class>
28     
29 </hibernate-mapping>

保存的时候。不论先保存哪一个。都是2条insert。因为dept是根据managers来生成主键的。

posted @ 2017-11-08 21:46  陆伟  阅读(228)  评论(0编辑  收藏  举报