Hibernate中可选的一对一映射 [英] Optional one-to-one mapping in Hibernate

查看:133
本文介绍了Hibernate中可选的一对一映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在hibernate hbm文件中创建可选的一对一映射?例如,假设我有一个用户和一个last_visited_pa​​ge表。用户可能有也可能没有last_visited页面。这是我目前在hbm文件中的一对一映射:

How do I create an optional one-to-one mapping in the hibernate hbm file? For example, suppose that I have a User and a last_visited_page table. The user may or may not have a last_visited page. Here is my current one-to-one mapping in the hbm file:

用户类:

<one-to-one name="lastVisitedPage" class="LastVisitedPage" cascade="save-update">

LastVisitedPage Class:

LastVisitedPage Class:

<one-to-one name="user" class="user" constrained="true" />

以上示例不允许创建没有上次访问过的页面的用户。新创建的用户尚未访问任何页面。如何更改hbm映射以使userPrefs映射可选?

The above example does not allow the creation of a user who does not have a last visited page. A freshly created user has not visited any pages yet. How do I change the hbm mapping to make the userPrefs mapping optional?

推荐答案

据我所知,Hibernate不支持可选的(请参阅 HHH-2007 ),因此您将拥有使用伪造的多对一来替代 not-null =false

To my knowledge, Hibernate doesn't support optional one-to-one (see HHH-2007) so you'll have to use a fake many-to-one with not-null="false" instead.

这篇关于Hibernate中可选的一对一映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆