休眠级联

发布于 2024-08-27 14:35:06 字数 695 浏览 6 评论 0原文

Hibernate 逆向工程生成的所有内容都是这样的,

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "column_id")
    public Itinerary getColumnId() {
        return this.columnId;
    }

我想要这样的场景:当会话刷新时,首先根据 FK 约束保存所有构造的子对象,然后保存父对象。

当然,孩子们需要首先被拯救(自动!),因为存在 FK 约束。

您会告诉我:有一个 CASCADE 选项,但如何将它与 JPA 一起使用?

我尝试添加这样的级联:

    @ManyToOne(fetch = FetchType.LAZY, cascade=CascadeType.PERSIST)
    @JoinColumn(name = "column_id")
    public Itinerary getColumnId() {
        return this.columnId;
    }

对我不起作用。

首先告诉我:这个指令应该注释什么以及如何让它发挥作用。

我收到“无法添加或更新子行:外键约束失败”异常。

事实上,我不想手工坚持一切!只构造一个对象并且 坚持下去!

要注释什么,使用什么指令以及如何注释?

All what Hibernate reverse engineering generates is something like this

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "column_id")
    public Itinerary getColumnId() {
        return this.columnId;
    }

I want this scenario: when session flushes, first all constructed childs were saved, then the parent object, according to FK constraints.

Of course, children need to be saved first (automatically!), 'cause there are FK constraints.

You'd tell me: there's a CASCADE option, but how to use it with JPA?

I tried adding cascade like this:

    @ManyToOne(fetch = FetchType.LAZY, cascade=CascadeType.PERSIST)
    @JoinColumn(name = "column_id")
    public Itinerary getColumnId() {
        return this.columnId;
    }

Does not work for me.

Tell me first: what should be annotated with this directive and how to get it worked.

I'm getting "Cannot add or update a child row: a foreign key constraint fails" exception.

And indeed, I do not want to persist everything by hand ! Only construct ONE object and
persist it!

What to annotate, what directive to use and how?

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

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

发布评论

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

评论(3

就像说晚安 2024-09-03 14:35:06

尝试将级联注释放在映射的父端,例如

@OneToMany(cascade = { CascadeType.PERSIST, 
                       CascadeType.MERGE, 
                       CascadeType.REMOVE },
           mappedBy = "children")
private Set<Children> children = new HashSet<Children>();

您可能需要也可能不需要所有这些级联选项 - 选择您的选择。

这是参考页面< /a> 以防万一。

Try putting the cascade annotation to the parent end of the mapping, like

@OneToMany(cascade = { CascadeType.PERSIST, 
                       CascadeType.MERGE, 
                       CascadeType.REMOVE },
           mappedBy = "children")
private Set<Children> children = new HashSet<Children>();

You may or may not need all those cascading options - pick your choice.

Here is a reference page just in case.

别想她 2024-09-03 14:35:06

你真正需要的是

cascade=CascadeType.SAVE_UPDATE

但这不是 JPA 的一部分。所以你可以使用它:

cascade=CascadeType.ALL

它将包含 SAVE_UPDATE (使用 Hibernate 实现)。但它可能包括您不喜欢的其他级联。

What you really need is

cascade=CascadeType.SAVE_UPDATE

But thats not part of JPA. So you can use this instead:

cascade=CascadeType.ALL

It will include SAVE_UPDATE (with the Hibernate implementation). But it may include other cascades you don't like.

晚雾 2024-09-03 14:35:06

您应该结合 JPA 和 Hibernate 的私有注释。请参阅文档

You should combine JPA and Hibernate's private annotations. See documentation.

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