java 休眠一对零或一映射

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/841354/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 14:00:40  来源:igfitidea点击:

Hibernate one to zero or one mapping

javahibernate

提问by Boden

I'm trying to map a one to "zero or one" relationship in Hibernate. I think I may have found a way using a many-to-one.

我试图在 Hibernate 中映射一个到“零或一”的关系。我想我可能已经找到了一种使用多对一的方法。

class A {
  private B b;
  // ... getters and setters
}

class B {
  private A a;
}

Class A's mapping specifies:

A 类的映射指定:

<many-to-one name="b" class="B" 
insert="false" update="false" 
column="id" unique="true"/>

and Class B's mapping specifies:

和 B 类的映射指定:

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

What I would like is for b to be null when no matching row for B was found in the database. So I can do this (in class A):

当在数据库中找不到与 B 匹配的行时,我希望 b 为空。所以我可以这样做(在 A 类中):

if (b == null)

However, it seems that b is never null.

但是,似乎 b 永远不会为空。

What can I do about this?

我该怎么办?

采纳答案by Boden

The answer was to add not-found="ignore" to the many-to-one statement in A:

答案是在 A 中的多对一语句中添加 not-found="ignore":

<many-to-one name="b" class="B" not-found="ignore" insert="false" update="false" column="id" unique="true"/>

I tried simply adding lazy="false" to B as Rob H recommended, but that resulted in a HibernateObjectRetrievalFailureException everytime I loaded an A that had no B.

我尝试按照 Rob H 的建议简单地将 lazy="false" 添加到 B,但是每次我加载没有 B 的 A 时都会导致 HibernateObjectRetrievalFailureException。

See this thread for more information:

请参阅本主题以获取更多信息:

https://forum.hibernate.org/viewtopic.php?p=2269784&sid=5e1cba6e2698ba4a548288bd2fd3ca4e

https://forum.hibernate.org/viewtopic.php?p=2269784&sid=5e1cba6e2698ba4a548288bd2fd3ca4e

回答by Tony

Like Boden said, the answer is to add not-found="ignore"to the many-to-one statement in A. Doing this with annotation:

就像 Boden 说的,答案是添加not-found="ignore"到 A 中的多对一语句中。用注解来做到这一点:

In Class A:

在 A 类中:

@ManyToOne
@Cascade({ CascadeType.ALL })
@JoinColumn(name = "Id")
@NotFound(action=NotFoundAction.IGNORE)
private B b

in Class B:

B类:

@Id
@GeneratedValue(generator = "myForeignGenerator")
@org.hibernate.annotations.GenericGenerator(
    name = "myForeignGenerator",
    strategy = "foreign",
    parameters = @Parameter(name = "property", value = "a")
)
private Long subscriberId;

@OneToOne(mappedBy="b")
@PrimaryKeyJoinColumn
@NotFound(action=NotFoundAction.IGNORE)
private A a;

回答by Rob H

Try setting lazy="false" on the many-to-one element. That should force Hibernate to try to fetch the association ("B") when the first object ("A") is loaded. The property in "A" will either be initialized with a real instance of "B" or null.

尝试在多对一元素上设置 lazy="false"。这应该会强制 Hibernate 在加载第一个对象(“A”)时尝试获取关联(“B”)。“A”中的属性将使用“B”的真实实例或 null 进行初始化。