数据库模型

  一般对一对一的关系而言,会存在一个主从关系。如 人 与 身份证,就是一个一对一关系, 人 是主,身份证 是从

Person

  PK:id

    name

    age

Id_Card

  PK、FK:id

    useful_life

一般在设计的时候,对于主表的id,由自增长或序列产生,从表的主键id可以直接使用主表的主键id,不需要自己重新生成


Domain

 package org.zln.hibernate.domain;

 /**
* Created by coolkid on 2015/6/20 0020.
*/
public class Person {
private int id;
private String name;
private IdCard idCard;
... }
 package org.zln.hibernate.domain;

 /**
* Created by coolkid on 2015/6/20 0020.
*/
public class IdCard {
private int id;
private String usefulLife;//证件有效期
private Person person;
...
}

XML

Person.hbm.xml

 <?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="org.zln.hibernate.domain"> <class name="Person" table="person">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="name" column="name"/>
<!--一对一关系映射-->
<one-to-one name="idCard" />
</class> </hibernate-mapping>

IdCard.hbm.xml

 <?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="org.zln.hibernate.domain"> <class name="IdCard" table="id_card">
<id name="id" column="id">
<!--主键依赖类中的属性-->
<generator class="foreign">
<!--外键从person属性中引用过来 从person 的 getId 方法获取-->
<param name="property">person</param>
</generator>
</id>
<property name="usefulList" column="useful_life"/>
<!--一对一关系映射-->
<one-to-one name="person" />
</class> </hibernate-mapping>

Dao

 package org.zln.hibernate.dao;

 import org.hibernate.Session;
import org.hibernate.Transaction;
import org.zln.hibernate.domain.IdCard;
import org.zln.hibernate.domain.Person;
import org.zln.hibernate.utils.HibernateUtils; /**
* Created by coolkid on 2015/6/20 0020.
*/
public class PersonDao { /**
* 保存Person及其IdCard
* @param person 人
* @param idCard 身份证
*/
public void savePerson(Person person,IdCard idCard){
Session session = null;
Transaction transaction = null;
try {
session = HibernateUtils.getSession();
transaction = session.beginTransaction(); person.setIdCard(idCard);
idCard.setPerson(person); session.save(person);
session.save(idCard); transaction.commit();
}finally {
if (session != null){
session.close();
}
}
} /**
* 通过id查询person
* @param person person
* @return Person
*/
public Person getPerson(Person person){
Session session = null;
Person person1 = null;
try {
session = HibernateUtils.getSession();
person1 = (Person) session.get(Person.class,person.getId());
}finally {
if (session != null){
session.close();
}
}
return person1;
}
}

另一种一对一关系的实现,就是使用多对一的方法,但是对“多”加唯一性约束

在配置IdCard的name属性时,使用<many-to-one>标签,对应的column是person_id,设置unique="true",这样也实现了一对一关系映射

 <?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="org.zln.hibernate.domain"> <class name="IdCard" table="id_card">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="usefulList" column="useful_life"/>
<many-to-one name="person" column="person_id" unique="true" not-null="true"/>
</class> </hibernate-mapping>
05-18 18:43