Hibernate初学之一对一双向、单向
public class Citizen { private int id; private String name; private IdCard idcard;}public class IdCard { private int id; private String no; }方式一:基于唯一外键Citizen.hbm.xml<hibernate-mapping package="com.hbsi.one2one"> <class name="Citizen" table="citizen"> <id name="id" column="id"> <generator class="native"/> </id> <property name="name" column="name" type="string"/> <many-to-one name="idcard" column="id_card" unique="true" cascade="save-update"/> </class></hibernate-mapping>测试:@Test public void add(){ Session session = HibernateSessionFactory.getSession(); session.beginTransaction(); IdCard idcard = new IdCard(); idcard.setNo("111293dddd"); Citizen c1 = new Citizen(); c1.setName("abc"); c1.setIdcard(idcard); session.save(c1); session.getTransaction().commit(); HibernateSessionFactory.closeSession(); }方式二:基于主键Citizen.hbm.xml:<hibernate-mapping package="com.hbsi.one2one_pk"> <class name="Citizen" table="citizen_pk"> <id name="id" column="id"> <generator class="foreign"> <param name="property">idcard</param> </generator> </id> <property name="name" column="name" type="string"/> <one-to-one name="idcard" constrained="true" cascade="all"></one-to-one> </class></hibernate-mapping>测试:@Test public void add(){ Session session = HibernateSessionFactory.getSession(); session.beginTransaction(); IdCard idcard = new IdCard(); idcard.setNo("1234567"); Citizen c1 = new Citizen(); c1.setName("zhangsan"); c1.setIdcard(idcard); session.save(c1); session.getTransaction().commit(); HibernateSessionFactory.closeSession(); }双向一对一:基于唯一外键; 基于主键public class Citizen { private int id; private String name; private IdCard idcard;}public class IdCard { private int id; private String no; private Citizen citizen;}对于基于唯一外键的一对一关联,其外键可以放在任意一端,在需要存放外键的一端,使用many-to-one元素,添加unqiue=true;没有存放外键的那一端需要使用one-to-one元素,此时还需要通过property-ref属性来指定这一端只是关联另一端,不需要额外的列或外键,由hibernate管理这个关系。Citizen.hbm.xml:<hibernate-mapping package="com.hbsi.one2oneboth_fk"> <class name="Citizen" table="citizen"> <id name="id" column="id"> <generator class="native"></generator> </id> <property name="name" column="name" type="string"/> <many-to-one name="idcard" unique="true" column="id_card" cascade="all"/> </class></hibernate-mapping>IdCard.hbm.xml:<hibernate-mapping package="com.hbsi.one2oneboth_fk"> <class name="IdCard" table="idcard"> <id name="id" column="id"> <generator class="native"/> </id> <property name="no" column="no" type="string"/> <one-to-one name="citizen" property-ref="idcard"></one-to-one> </class></hibernate-mapping>测试@Test public void add(){ Session session = HibernateSessionFactory.getSession(); session.beginTransaction(); IdCard idcard = new IdCard(); idcard.setNo("33333333"); Citizen c = new Citizen(); c.setName("wwwww"); c.setIdcard(idcard); idcard.setCitizen(c); session.save(c); session.getTransaction().commit(); HibernateSessionFactory.closeSession(); }基于主键,一端的主键使用foreign策略来参照另一端的主键来生成。这样两端就具有相同的主键值。Citizen.hbm.xml:<hibernate-mapping package="com.hbsi.one2oneboth_fk"> <class name="Citizen" table="citizen"> <id name="id" column="id"> <generator class="foreign"> <param name="property">idcard</param> </generator> </id> <property name="name" column="name" type="string"/> <one-to-one name="idcard" constrained="true" cascade="all"></one-to-one> </class></hibernate-mapping>IdCard.hbm.xml:<hibernate-mapping package="com.hbsi.one2oneboth_fk"> <class name="IdCard" table="idcard"> <id name="id" column="id"> <generator class="native"/> </id> <property name="no" column="no" type="string"/> <one-to-one name="citizen"></one-to-one> </class></hibernate-mapping>测试同上