日期:2014-05-16 浏览次数:20474 次
一对一主键关联
数据表为:夫妻表
?POJO 为:
?映射文件为:
Husband.hmb.xml
<class?
???????????name="com.demo.hibernate.beans.Husband"?
?????????? table="t_husband"
????????? ?lazy="false"
>
??? <id name="id"?
?????????? type="java.lang.Integer"?
?????????? column="id"
??? >
??????????<generator class="increment" />
??? </id>
??? <property name="name"
???????????????type="java.lang.String"
???????????????column="name"
???????????????length="50"
??? />
?????<one-to-one name="myWife" class="com.demo.hibernate.beans.Wife"?cascade="all"/>?<!--配置映射-->
</class>
Wife.hbm.xml
<class?
??? name="com.demo.hibernate.beans.Wife"?
??? table="t_wife"
??? lazy="false"
>
??? <id
??????? name="id"
??????? type="java.lang.Integer"
??????? column="id"
??? >
??????? <generator class="foreign" >??? <!--主键作为与Husband 对象关联的外键-->
??????? ?????????????<param name="property">myHusband</param>?<!--主键的值是由Husband的主键赋给-->
???????? </generator>
??? </id>
??? <property
?????????????? ?name="name"
??????????????? type="java.lang.String"
??????????????? column="name"
????????????????length="50"
??? />
????<one-to-one name="myHusband" class="com.demo.hibernate.beans.Husband"?cascade="all">?<!--配置关联映射-->
</class>
插入一条记录:
? Husband husband =?new?THusband();
??husband.setName("杨过");
??
??Wife wife =?new?Wife();
??wife.setName("小龙女");
??
??