Java Hibernate 中可选的一对一映射
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2784228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Optional one-to-one mapping in Hibernate
提问by hibernate
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:
如何在 hibernate hbm 文件中创建可选的一对一映射?例如,假设我有一个 User 和一个 last_visited_page 表。用户可能有也可能没有 last_visited 页面。这是我当前在 hbm 文件中的一对一映射:
User Class:
用户类别:
<one-to-one name="lastVisitedPage" class="LastVisitedPage" cascade="save-update">
LastVisitedPage Class:
LastVisitedPage 类:
<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?
上面的示例不允许创建没有上次访问页面的用户。新创建的用户尚未访问任何页面。如何更改 hbm 映射以使 userPrefs 映射可选?
采纳答案by Pascal Thivent
回答by Erwin Smout
If a user has at most one last_visited page, then there are two cases :
如果一个用户最多有一个 last_visited 页面,那么有两种情况:
(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.
(a) 某些给定用户没有 last_visited 页面,在这种情况下,last_visited_page 表中将没有该用户的任何元组,(b) 某些给定用户恰好有一个 last_visited 页面,在这种情况下,将只有一个元组用于这个用户在 last_visited_page 表中。
That should make it obvious that userid is a candidate key in your last-visited-page table.
这应该表明 userid 是上次访问页面表中的候选键。
And that should make it obvious that you should declare that key to the DBMS.
这应该表明您应该向 DBMS 声明该键。
回答by davidemm
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
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 ;-)
如果您使用注释,则可以使用 @NotFound(action=NotFoundAction.IGNORE) 注释,以免出现异常。只需确保您的代码检查空值,因为它现在可能不存在;-)
回答by JustDanyul
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
希望这可能有助于节省一些时间
回答by Tolomeo Nogo
Had the same issue, resolved with @OneToOne(optional = true)
on the User class (hibernate 5.2.17.Final)
有同样的问题,@OneToOne(optional = true)
在 User 类上解决了(hibernate 5.2.17.Final)