我是一个新的bie的hibernate的世界,你可以请指教,我正在通过Hibernate的一对一关系, 根据我的分析,可以建立一对一的hibernate关系通过三种方式..关于一对一映射休眠
1)Through Join concept
2)Same primary key in both the tables
3)Primary key and foriegn key relationship in both the tables
请指教,以上三种方式来实现一对一的映射是正确的还是我失去了一些东西,然后请指教,也请提醒,以下HBM映射文件,我我使用的是正确的one.if然后请告知。
1)通过加入概念: -
一种方式来实现,以一对一的关系是通过接合的概念,以下被用于在两个表的是
<hibernate-mapping>
<class name="mypack.Person" table="person21">
<id name="personId" type="int">
<generator class="increment">
</generator>
</id>
<property name="name"/>
<join table="personAddress">
<key column="personId"/>
<many-to-one name="address" class="mypack.Address" column="addressId" unique="true" cascade="all"/>
</join>
</class>
<class name="mypack.Address" table="address21">
<id name="id" column="addressId" type="int">
<generator class="increment"/>
</id>
<property name="city"/>
<property name="state"/>
</class>
</hibernate-mapping>
2)相同的主键的xml : -
使用在这两个表中相同的主键,以下HBM被用于该
<class name="mypack.Address" table="address31">
<id name="id" column="addressId" type="int">
<generator class="increment"/>
</id>
<property name="city"/>
<property name="state"/>
</class>
<class name="mypack.Person" table="person31">
<id name="personId" type="int">
<generator class="foreign">
<param name="property">address</param>
</generator>
</id>
<property name="name"/>
<one-to-one name="address" class="mypack.Address"/>
</class>
</hibernate-mapping>
3)普瑞玛ry键和foriegn在两个表中的关键关系: -
主键在一个表中,foriegn键在另一个表中。下面是这个hbm
<hibernate-mapping>
<class name="mypack.Person">
<id name="personId" type="int">
<generator class="increment"/>
</id>
<property name="name"/>
<many-to-one name="address" class="mypack.Address" column="addressId" unique="true" cascade="all"/>
</class>
<class name="mypack.Address">
<id name="id" column="addressId" type="int">
<generator class="increment"/>
</id>
<property name="city"/>
<property name="state"/>
</class>
</hibernate-mapping>
伙计们请指教。
你有没有考虑使用注释,而不是XML映射?如果你是新手,可能会更容易,并且有很多好的教程可以开始。 – Magnilex 2013-02-28 17:37:36
@MagnusTengdahl谢谢,是的,我必须选择XML,请告知这个hbm xml映射是正确的还是他们需要任何修改 – user2094103 2013-03-01 02:23:28
我不使用xml映射和休眠,我有强烈的偏好使用注释。但是您正尝试执行简单的一对一关系,只需按照以下教程进行操作即可:http://www.dzone.com/tutorials/java/hibernate/hibernate-example/hibernate-mapping-one-to- one-1.html – Magnilex 2013-03-01 07:35:35