Hibernate 中可选的一对一映射

发布于 2024-08-31 13:33:38 字数 450 浏览 7 评论 0原文

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

用户类:

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

LastVisitedPage 类:

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

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

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:

User Class:

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

LastVisitedPage Class:

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

黒涩兲箜 2024-09-07 13:33:40

据我所知,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.

-小熊_ 2024-09-07 13:33:40

今天花了大部分时间尝试做类似的事情,终于找到了以下解决方案(以防万一这可能对其他人有用)

@OneToOne
@JoinColumn(name="ClassA_Id", referencedColumnName="ClassB_Id", nullable=true)

希望可以帮助节省某人一些时间

Just spend most of the day today trying to do a similar thing, finally found the following solution (just in-case this might be useful for other people)

@OneToOne
@JoinColumn(name="ClassA_Id", referencedColumnName="ClassB_Id", nullable=true)

Hope that might help save somebody some time

无法回应 2024-09-07 13:33:40

有同样的问题,通过 User 类上的 @OneToOne(Optional = true) 解决(hibernate 5.2.17.Final)

Had the same issue, resolved with @OneToOne(optional = true) on the User class (hibernate 5.2.17.Final)

べ映画 2024-09-07 13:33:40

我遇到了类似的问题,但是使用注释。 Google 把我带到这里,所以如果其他人发现自己处于同样的情况,这对我有用:

http://opensource.atlassian.com/projects/hibernate/browse/ANN-725

如果您使用注释,则可以使用 @NotFound(action=NotFoundAction.IGNORE) 注释,这样您就不会没有例外。只需确保您的代码检查空值,因为它现在可能不存在;-)

I was having a simliar problem, but using annotations. Google brought me here, so if anyone else finds themselves in the same sitatuions, this worked for me:

http://opensource.atlassian.com/projects/hibernate/browse/ANN-725

If you're using annotations, you can use the @NotFound(action=NotFoundAction.IGNORE) annotation so that you don't get an exception. Just make sure your code checks for nulls because it's now might not be there ;-)

离笑几人歌 2024-09-07 13:33:40

如果一个用户至多有一个last_visited页面,则有两种情况:

(a)某个给定用户没有last_visited页面,在这种情况下,last_visited_pa​​ge表中不会有该用户的任何元组,
(b) 某个给定用户恰好具有一个last_visited页面,在这种情况下,该用户在last_visited_pa​​ge表中将恰好有一个元组。

这应该表明 userid 是上次访问页表中的候选键。

这应该表明您应该向 DBMS 声明该密钥。

If a user has at most one last_visited page, then there are two cases :

(a) some given user has no last_visited page, in which case there will not be any tuple for this user in the last_visited_page table,
(b) some given user has exactly one last_visited page, in which case there will be exactly one tuple for this user in the last_visited_page table.

That should make it obvious that userid is a candidate key in your last-visited-page table.

And that should make it obvious that you should declare that key to the DBMS.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文