休眠级联
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试将级联注释放在映射的父端,例如
您可能需要也可能不需要所有这些级联选项 - 选择您的选择。
这是参考页面< /a> 以防万一。
Try putting the cascade annotation to the parent end of the mapping, like
You may or may not need all those cascading options - pick your choice.
Here is a reference page just in case.
你真正需要的是
但这不是 JPA 的一部分。所以你可以使用它:
它将包含 SAVE_UPDATE (使用 Hibernate 实现)。但它可能包括您不喜欢的其他级联。
What you really need is
But thats not part of JPA. So you can use this instead:
It will include SAVE_UPDATE (with the Hibernate implementation). But it may include other cascades you don't like.
您应该结合 JPA 和 Hibernate 的私有注释。请参阅文档。
You should combine JPA and Hibernate's private annotations. See documentation.