(双向关联Person<—>IdCard),采用<one-to-one name="person" property-ref="idCard"/>标签中的property-ref属性为关系字段的名称。
。
Person.java |
publicclass Person { privateintid; private String name; private IdCard idCard; } |
IdCard.java |
publicclass IdCard { privateintid; private String cardNo; private Person person; } |
Person.hbm.xml |
<hibernate-mapping> <class name="com.xatrans.test.hibernate.vo.Person" table="tbl_person"> <id name="id" length="10"> <generator class="native"/> </id> <property name="name" length="30"/> <many-to-one name="idCard" unique="true"/> </class> </hibernate-mapping> |
IdCard.hbm.xml |
<hibernate-mapping> <class name="com.xatrans.test.hibernate.vo.IdCard" table="tbl_idcard"> <id name="id" length="10"> <generator class="native"/> </id> <property name="cardNo" length="30"/> <one-to-one name="person" property-ref="idCard"/> </class> </hibernate-mapping> |
*在开发中,如果遇到一对一关联关系,建议采用一对一唯一外键关联,更具有扩展性,如果需要修改成多对一,只需要把unique="true"属性去掉。