Hibernate ORM - 一对一外键关联关系

 

  对于持久化实体间一对一的关联关系来说,在代码层面来看,没有什么太大的问题,如果你想建立一个一对一的单向关联关系,那么只需要在代码中增加一个对对方实体的引用即可,如果你想建立一个一对一的双向关联关系,那么只需要在双方实体中增加对对方实体的引用即可。而从数据表创建的层面上来看,则会根据你对持久化实体的配置文件的不同配置而有所不同。所以对于一对一关联关系,重点在于你在数据表层面上想怎样实现这样的关联关系,主要有两种方式,一种方式是在某一方的表中增加一个外键标识符字段,用来保持双方的一对一关联关系,还有一种方式则是将某一方实体的主键字段同时作为外键字段用来保持两者的一对一关联关系。具体的做法则会因你的项目的具体需求而定。而至于两者的差异则显而易见,主要体现在数据表上,第一种方式会比第二种方式多配置一个字段,而第二种方式则有一个好处就是两者的主键标识符字段会始终保持一致性。下面就来看看一对一外键双向关联关系。

 

  一。Husband

 

package com.orm.model;

/**
 * Created by IntelliJ IDEA.
 * User: Zhong Gang
 * Date: 10/18/11
 * Time: 3:23 PM
 */
public class Husband extends DomainObject {
    private String name;

    private Wife wife;

    public Husband() {
    }

    public Husband(String name, Wife wife) {
        this.name = name;
        this.wife = wife;
    }
}

 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping default-access="field">

    <class name="com.orm.model.Husband" table="husband">
        <id name="id" column="id" type="java.lang.Integer">
            <generator class="native"/>
        </id>
        <property name="name" column="name" type="java.lang.String"/>

        <many-to-one name="wife"  column="wifeid" class="com.orm.model.Wife" cascade="all" unique="true"/>
    </class>

</hibernate-mapping>

 

  二。Wife

 

package com.orm.model;

/**
 * Created by IntelliJ IDEA.
 * User: Zhong Gang
 * Date: 10/18/11
 * Time: 3:23 PM
 */
public class Wife extends DomainObject {
    private String name;

    private Husband husband;

    public Wife(String name) {
        this.name = name;
    }

    public Wife() {
    }
}

 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping default-access="field">

    <class name="com.orm.model.Wife" table="wife">
        <id name="id" column="id" type="java.lang.Integer">
            <generator class="native"/>
        </id>
        <property name="name" column="name" type="java.lang.String"/>

        <one-to-one name="husband" class="com.orm.model.Husband"/>
    </class>

</hibernate-mapping>

 

  三。测试类

 

package com.orm;

import com.orm.model.Husband;
import com.orm.model.Wife;
import com.orm.service.CoupleService;
import junit.framework.TestCase;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by IntelliJ IDEA.
 * User: Zhong Gang
 * Date: 10/18/11
 * Time: 3:40 PM
 */
public class HibernateOneToOneSingleDirectionTest extends TestCase {
    private CoupleService coupleService;

    @Override
    public void setUp() throws Exception {
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:testDataSource.xml");
        coupleService = (CoupleService) context.getBean("coupleService");
    }

    public void testOneToOneSingleDirection() throws Exception {
        Wife wife = new Wife("wife");

        Husband husband = new Husband("husband", wife);

        coupleService.saveOrUpdate(husband);
    }

}

 

  四。测试结果截图

 

  Hibernate ORM - 一对一外键关联关系

 

  Hibernate ORM - 一对一外键关联关系

 

  这就是一个典型的一对一外键双向关联关系的实现方式。如果你需要的不是双向的而是单向的,比如说在这里你需要的是Husband持有Wife的引用,而Wife不能持有Husband的引用,那么具体做法就是删除Wife代码中对Husband的引用,并且删除Wife配置文件中的one-to-one标签。相反的话你则需要将Husband代码中对Wife的引用删除,并且将Husband配置文件中的many-to-one标签删除,并且将Wife的配置文件中的one-to-one标签以<many-to-one name="husband"  column="husbandid" class="com.orm.model.Husband" cascade="all" unique="true"/>进行替换。最后附上源码附件。