数据库模型
一般对一对一的关系而言,会存在一个主从关系。如 人 与 身份证,就是一个一对一关系, 人 是主,身份证 是从
Person
PK:id
name
age
Id_Card
PK、FK:id
useful_life
一般在设计的时候,对于主表的id,由自增长或序列产生,从表的主键id可以直接使用主表的主键id,不需要自己重新生成
Domain
1 package org.zln.hibernate.domain; 2 3 /** 4 * Created by coolkid on 2015/6/20 0020. 5 */ 6 public class Person { 7 private int id; 8 private String name; 9 private IdCard idCard; ...43 }
1 package org.zln.hibernate.domain; 2 3 4 /** 5 * Created by coolkid on 2015/6/20 0020. 6 */ 7 public class IdCard { 8 private int id; 9 private String usefulLife;//证件有效期 10 private Person person; ... 43 }
XML
Person.hbm.xml
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping package="org.zln.hibernate.domain"> 6 7 <class name="Person" table="person"> 8 <id name="id" column="id"> 9 <generator class="native"/> 10 </id> 11 <property name="name" column="name"/> 12 <!--一对一关系映射--> 13 <one-to-one name="idCard" /> 14 </class> 15 16 </hibernate-mapping>
IdCard.hbm.xml
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping package="org.zln.hibernate.domain"> 6 7 <class name="IdCard" table="id_card"> 8 <id name="id" column="id"> 9 <!--主键依赖类中的属性--> 10 <generator class="foreign"> 11 <!--外键从person属性中引用过来 从person 的 getId 方法获取--> 12 <param name="property">person</param> 13 </generator> 14 </id> 15 <property name="usefulList" column="useful_life"/> 16 <!--一对一关系映射--> 17 <one-to-one name="person" /> 18 </class> 19 20 </hibernate-mapping>
Dao
1 package org.zln.hibernate.dao; 2 3 import org.hibernate.Session; 4 import org.hibernate.Transaction; 5 import org.zln.hibernate.domain.IdCard; 6 import org.zln.hibernate.domain.Person; 7 import org.zln.hibernate.utils.HibernateUtils; 8 9 /** 10 * Created by coolkid on 2015/6/20 0020. 11 */ 12 public class PersonDao { 13 14 /** 15 * 保存Person及其IdCard 16 * @param person 人 17 * @param idCard 身份证 18 */ 19 public void savePerson(Person person,IdCard idCard){ 20 Session session = null; 21 Transaction transaction = null; 22 try { 23 session = HibernateUtils.getSession(); 24 transaction = session.beginTransaction(); 25 26 person.setIdCard(idCard); 27 idCard.setPerson(person); 28 29 session.save(person); 30 session.save(idCard); 31 32 transaction.commit(); 33 }finally { 34 if (session != null){ 35 session.close(); 36 } 37 } 38 } 39 40 /** 41 * 通过id查询person 42 * @param person person 43 * @return Person 44 */ 45 public Person getPerson(Person person){ 46 Session session = null; 47 Person person1 = null; 48 try { 49 session = HibernateUtils.getSession(); 50 person1 = (Person) session.get(Person.class,person.getId()); 51 }finally { 52 if (session != null){ 53 session.close(); 54 } 55 } 56 return person1; 57 } 58 }
另一种一对一关系的实现,就是使用多对一的方法,但是对“多”加唯一性约束
在配置IdCard的name属性时,使用<many-to-one>标签,对应的column是person_id,设置unique="true",这样也实现了一对一关系映射
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping package="org.zln.hibernate.domain"> 6 7 <class name="IdCard" table="id_card"> 8 <id name="id" column="id"> 9 <generator class="native"/> 10 </id> 11 <property name="usefulList" column="useful_life"/> 12 <many-to-one name="person" column="person_id" unique="true" not-null="true"/> 13 </class> 14 15 </hibernate-mapping>