Hibernate 中可选的一对一映射
如何在 hibernate hbm 文件中创建可选的一对一映射?例如,假设我有一个 User 和一个 last_visited_page 表。用户可能有也可能没有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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
据我所知,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
withnot-null="false"
instead.今天花了大部分时间尝试做类似的事情,终于找到了以下解决方案(以防万一这可能对其他人有用)
希望可以帮助节省某人一些时间
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)
Hope that might help save somebody some time
有同样的问题,通过 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)我遇到了类似的问题,但是使用注释。 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 ;-)
如果一个用户至多有一个last_visited页面,则有两种情况:
(a)某个给定用户没有last_visited页面,在这种情况下,last_visited_page表中不会有该用户的任何元组,
(b) 某个给定用户恰好具有一个last_visited页面,在这种情况下,该用户在last_visited_page表中将恰好有一个元组。
这应该表明 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.