(十)Hibernate的一对一关联关系

一、概述

 

 

我们以Company与Address类为例,介绍映射一对一关联关系的方法。 
一对一的关联方法有两种 
-按照外键映射:两个表任意一个表定义一个外键,来关联另一个表。 
-按照主键映射:一个表的主键同时作为外键,和另一个表的主键保持一致。

 

二、按照外键映射

(1)创建实体类

            public class Company {
                private Integer id;
                private String name;
                private Address address;
                public Integer getId() {
                    return id;
                }
                public void setId(Integer id) {
                    this.id = id;
                }
                public String getName() {
                    return name;
                }
                public void setName(String name) {
                    this.name = name;
                }
                public Address getAddress() {
                    return address;
                }
                public void setAddress(Address address) {
                    this.address = address;
                }

            } 

            public class Address {
                private Integer id;
                private String name;
                private Company company;
                public Integer getId() {
                    return id;
                }
                public void setId(Integer id) {
                    this.id = id;
                }
                public String getName() {
                    return name;
                }
                public void setName(String name) {
                    this.name = name;
                }
                public Company getCompany() {
                    return company;
                }
                public void setCompany(Company company) {
                    this.company = company;
                }

            } 

  (2)我们配置映射文件,我们在Company这一方设置外键来关联Address 有外键的一方要使用<many-to-one>元素来配置。

        Company.hbm.xml
         <hibernate-mapping >
            <class name="com.cad.domain.Company" table="company">
                <id name="id" column="id">
                    <generator class="native"></generator>
                </id>
                <property name="name" column="name"></property> 
                <!--column指定外键,unique指定外键唯一约束,设为true,就可以表达Company和Address对象之间的一对一关联-->
                <many-to-one name="address" class="com.cad.domain.Address" column="aid" unique="true"></many-to-one>
            </class>
         </hibernate-mapping>   

         Address.hbm.xml 
         <hibernate-mapping >
            <class name="com.cad.domain.Address" table="address">
                <id name="id" column="id">
                    <generator class="native"></generator>
                </id>
                <property name="name" column="name"></property> 
                <!--property-ref属性指定通过从Company的address属性来查找自己-->
                <one-to-one name="Company" class="com.cad.domain.Company" property-ref="address"></one-to-one>
            </class>
         </hibernate-mapping>  

  测试:

                 public class Demo {

                    private Session session;

                    @Test
                    public void test() {
                        //读取配置文件
                        Configuration conf=new Configuration().configure();

                        //根据配置创建factory
                        SessionFactory sessionfactory=conf.buildSessionFactory();
                        session = sessionfactory.openSession(); 
                         Transaction ts=session.beginTransaction();
                        Company c=new Company();
                        c.setName("百度");
                        Address a=new Address();
                        a.setName("深圳");
                        c.setAddress(a);
                        a.setCompany(c);

                        session.save(a);
                        session.save(c); 

                        ts.commit();
                        session.close();
                        sessionfactory.close();
                    }

                }

            默认情况下,一对一关联采用迫切左外连接检索策略。

  

三、按照主键映射

 

    实体类不变,我们编写一下配置文件
    address的表的id字段既是主键又是外键.

        编写配置文件
        Company.hbm.xml 
         <hibernate-mapping >
            <class name="com.cad.domain.Company" table="company">
                <id name="id" column="id">
                    <generator class="native"></generator>
                </id>
                <property name="name" column="name"></property>
                <one-to-one name="address" class="com.cad.domain.Address" ></one-to-one>
            </class>
         </hibernate-mapping> 

        Address.hbm.xml 
         <hibernate-mapping >
            <class name="com.cad.domain.Address" table="address">
                <id name="id" column="id">
                    <!--必须使用foreign标识符生成策略,还要指定哪个对象共享OID-->
                    <generator class="foreign">
                        <param name="property">company</param>
                    </generator>
                </id>
                <property name="name" column="name"></property>
                <!--constrained属性设为true,说明主键同时作为外键-->
                <one-to-one name="company" class="com.cad.domain.Company" constrained="true"></one-to-one>
            </class>
         </hibernate-mapping>  

 

  

 

posted @ 2018-08-09 23:05  跃小云  阅读(187)  评论(0编辑  收藏  举报