hibernate中关于一对一关联的问题 补充一点,上面的constrained="true"应该是constrained="false" 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 有两种不同的一对一关联: 主键关联 惟一外键关联 主键关联不需要额外的表字段;两行是通过这种一对一关系相关联的,那么这两行就共享同样的主关键字值。所以如果你希望两个对象通过主键一对一关联,你必须确认它们被赋予同样的标识值! 比如说,对下面的Employee和Person进行主键一对一关联: <one-to-one name="person" class="Person"/><one-to-one name="employee" class="Employee" constrained="true"/>现在我们必须确保PERSON和EMPLOYEE中相关的字段是相等的。我们使用一个特别的称为foreign的Hibernate标识符生成器策略: <class name="person" table="PERSON"> <id name="id" column="PERSON_ID"> <generator class="foreign"> <param name="property">employee</param> </generator> </id> ... <one-to-one name="employee" class="Employee" constrained="true"/></class>一个刚刚保存的Person实例被赋予和该Person的employee属性所指向的Employee实例同样的关键字值。 另一种方式是一个外键和一个惟一关键字对应,上面的Employee和Person的例子,如果使这种关联方式,应该表达成: <many-to-one name="person" class="Person" column="PERSON_ID" unique="true"/>如果在Person的映射加入下面几句,这种关联就是双向的: <one-to-one name"employee" class="Employee" property-ref="person"/> 重点是这个: <generator class="foreign"> <param name="property">employee</param> </generator>你要使用foreign生成器才行。根据你的配置修改一下。 如果是外键关联,那么你在husband.hbm.xml的映射应该用<one-to-one name="wife" class="family.Wife" cascade="all" property-ref="husband"/>而不应该是:<one-to-one name="wife" class="family.Wife" cascade="all" constrained="true">要使用双向关联的话,在wife.hbm.xml中加上:<many-to-one name="husband" class="family.Husband" column="FK_HUSBAND_ID()此处是wife表中的外键字段" unique="true"/>同时在wife.hbm.xml的id属性中加上:unsaved-value,设置方法根据你的主键类型决定,如果你的主键是int或long型的,那么: <id name="id" column="PK_ID" type="long" length="10" unsaved-value="0"> <generator class="这里是主键生成器类型"> </generator> </id>如果是string型的,unsaved-value="null" to IceCraft(心淡情浓) 那个是主动方啊?是husband还是wife? 根据楼主的描述,husband是主的。 如何实现网页缩略图效果。。。 structs 中的isTokenValid函数第一次提交时返回false 大伙帮我作一个选择啊! java如何获取特殊设备信息 struts2中模型驱动怎样传值到实体类? 怎样用dwr和struts2实现省市级连?或者其他连带数据的例子请高手给一个! 我是个新手.问个cmp的问题 请问STRUTS该不该用呀,那位学过STRUTS大哥,帮小弟指点下困惑呀 关于hibernate中的多数据库的使用问题 怎样用java编程实现DNS服务器域名解析地时间? 请前辈们指教 用java org.apache.axis.wsdl.Java2WSDL 命令出错
<one-to-one name="employee" class="Employee" constrained="true"/>
现在我们必须确保PERSON和EMPLOYEE中相关的字段是相等的。我们使用一个特别的称为foreign的Hibernate标识符生成器策略: <class name="person" table="PERSON">
<id name="id" column="PERSON_ID">
<generator class="foreign">
<param name="property">employee</param>
</generator>
</id>
...
<one-to-one name="employee"
class="Employee"
constrained="true"/>
</class>
一个刚刚保存的Person实例被赋予和该Person的employee属性所指向的Employee实例同样的关键字值。 另一种方式是一个外键和一个惟一关键字对应,上面的Employee和Person的例子,如果使这种关联方式,应该表达成: <many-to-one name="person" class="Person" column="PERSON_ID" unique="true"/>
如果在Person的映射加入下面几句,这种关联就是双向的: <one-to-one name"employee" class="Employee" property-ref="person"/>
<generator class="foreign">
<param name="property">employee</param>
</generator>
你要使用foreign生成器才行。根据你的配置修改一下。
<one-to-one name="wife" class="family.Wife" cascade="all" property-ref="husband"/>
而不应该是:
<one-to-one name="wife" class="family.Wife" cascade="all" constrained="true">要使用双向关联的话,在wife.hbm.xml中加上:
<many-to-one name="husband" class="family.Husband" column="FK_HUSBAND_ID()此处是wife表中的外键字段" unique="true"/>
同时在wife.hbm.xml的id属性中加上:unsaved-value,设置方法根据你的主键类型决定,如果你的主键是int或long型的,那么:
<id name="id" column="PK_ID" type="long" length="10" unsaved-value="0">
<generator class="这里是主键生成器类型">
</generator>
</id>
如果是string型的,unsaved-value="null"
那个是主动方啊?是husband还是wife?