JPA(休眠)OneToMany 关系和 null

发布于 2024-11-06 03:43:15 字数 839 浏览 5 评论 0原文

我在 Resident 类和 ResidentInfo 类之间有 OneToMany 关系 ResidentInfo 不应该在没有 Resident 的情况下存在,但 ResidentInfo 不是必需的

这是我的课程:

public class Resident {
...
    @OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH}, mappedBy = "resident")
    @Cascade({org.hibernate.annotations.CascadeType.ALL})
    public List<ResidentInfo> infos;
}

public class ResidentInfo {
...
    @ManyToOne(optional = false)
    public Resident resident;
    public String label;
    public String value;
}

我是 JPA 的新手,也许我做错了事情。

问题是,当我尝试一次插入所有数据(同时创建居民及其信息)时,我遇到了空异常。

这是因为 ResidentInfo.resident.id 为 null(但我无法指定值,因为 Resident 是同时创建的!)

此外,即使先创建了 resident,我也会遇到此错误,但我不知道为什么:

detached entity passed to persist: models.ResidentInfo

那么如何解决这些问题呢?

I have a relation OneToMany between a class Resident and ResidentInfo
ResidentInfo is not supposed to exists without Resident, but ResidentInfo is not required

Here are my classes:

public class Resident {
...
    @OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH}, mappedBy = "resident")
    @Cascade({org.hibernate.annotations.CascadeType.ALL})
    public List<ResidentInfo> infos;
}

public class ResidentInfo {
...
    @ManyToOne(optional = false)
    public Resident resident;
    public String label;
    public String value;
}

I'm new to JPA and maybe I'm doing things wrong.

The issue is that when I try to insert datas all at once (create resident and its infos at the same time) I've got a null exception.

This is because ResidentInfo.resident.id is null (but I can't specify a value because Resident is created in the same time !)

Also even if the resident is created first, I have this error but I don't know why :

detached entity passed to persist: models.ResidentInfo

So how to resolve theses issues ?

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

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

发布评论

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

评论(1

晨曦慕雪 2024-11-13 03:43:15

为了解决您的问题,您应该首先在 ResidentInfo 类中提供一个 setter 方法来设置 info 实例所属的 Resident 对象。它可能看起来像这样 此外

public void setResident(Resident resident) {
    this.resident = resident;
}

,将 ResidentInfo 对象添加到 Resident 对象只能在 Resident 类中完成。您应该提供一个方法来将新的 ResidentInfo 对象添加到所包含的列表中。添加 ResidentInfo 对象的一部分是设置与其关联的 Resident 对象。代码可能如下所示

public void addResidentInfo(ResidentInfo residentInfo) {
    residentInfo.setResident(this);
    infos.add(residentInfo);
}

通过这些行,列表中的每个对象都包含对与其关联的 Resident 实例的引用。因此,当您保留所有对象时,就会设置必要的引用(以及任何所需的 ID)。

另外,您应该检查 JPA 注释中的 Cascade 信息。您提供了两个具有不同含义的不同注释。

To solve your problem you should provide at first a setter method in the class ResidentInfo to set the Resident object the info instance belongs to. It could look like this

public void setResident(Resident resident) {
    this.resident = resident;
}

Furthermore adding a ResidentInfo object to a Resident object should only be done in the Resident class. You should provide a method to add a new ResidentInfo object to the contained list. Part of adding the ResidentInfo object would be to set the Resident object that is associated with it. The code could look something like this

public void addResidentInfo(ResidentInfo residentInfo) {
    residentInfo.setResident(this);
    infos.add(residentInfo);
}

With these lines every object in the list contains a reference to the Resident instance that is associated with it. So when you persist all objects, the necessary references (as well as any required ID) are set.

On a side note you should check your Cascade information in the JPA annotation. You provided two different annotations with very different meaning.

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