hibernate之一对一

*、hibernate之一对一配置启动前提

现有实体person和实体idcard,两者关系为一对一

*、由person引用idcard的主键作为外键时

--在person的hbm配置文件中添加idcard_id字段

<property name="idcareId" type="java.lang.Integer">
	<column name="idcare_id">
		<comment>idcard主键ID</comment>
	</column>
</property>
注:保存操作时需要person.setIdcardID();来保存idcard的主键

   

当person需要管理idcard的实体时或者说懒加载该实体类时

--在person的实体类中添加idcare实体属性

private IdCare idcard;
get/set

--在person的hbm配置文件中添加many-to-one属性

<many-to-one name="idcard被管理实体对象" class="com.idcard" 
cascade="none" unique="true保证一对一的关键" insert="false" update="false" >
	<column name="idcared_id,person表中外键字段即idcard主键id" not-null="true" />
</many-to-one>

反之:则可以在idcard的实体类和hbm配置以上信息,若同时配置,也叫双向一对一关联

正确与否,还需商酌,不当之处望不吝赐教~

--------------------------------------------------小小Bug------------------------------------------------------

*、Could not initialize proxy - the owning Session was closed

详见:http://lbovinl.iteye.com/blog/2373492

猜你喜欢

转载自lbovinl.iteye.com/blog/2373483