如何修复 Hibernate 的“对象引用未保存的瞬态实例 - 在刷新之前保存瞬态实例"错误 [英] How to fix the Hibernate "object references an unsaved transient instance - save the transient instance before flushing" error
问题描述
当我使用 Hibernate 保存对象时收到以下错误
I receive following error when I save the object using Hibernate
object references an unsaved transient instance - save the transient instance before flushing
推荐答案
我相信这可能只是重复答案,但只是为了澄清,我在 @OneToOne
映射以及一个@OneToMany
.在这两种情况下,事实是我添加到 Parent
的 Child
对象尚未保存在数据库中.因此,当我将 Child
添加到 Parent
,然后保存 Parent
时,Hibernate 会抛出 " 对象引用未保存的瞬态实例- 在刷新之前保存瞬态实例"
保存父级时的消息.
I believe this might be just repeat answer, but just to clarify, I got this on a @OneToOne
mapping as well as a @OneToMany
. In both cases, it was the fact that the Child
object I was adding to the Parent
wasn't saved in the database yet. So when I added the Child
to the Parent
, then saved the Parent
, Hibernate would toss the "object references an unsaved transient instance - save the transient instance before flushing"
message when saving the Parent.
在 Parent's
引用上添加 cascade = {CascadeType.ALL}
到 Child
解决了这两种情况下的问题.这保存了 Child
和 Parent
.
Adding in the cascade = {CascadeType.ALL}
on the Parent's
reference to the Child
solved the problem in both cases. This saved the Child
and the Parent
.
很抱歉重复回答,只是想为大家进一步澄清.
Sorry for any repeat answers, just wanted to further clarify for folks.
@OneToOne(cascade = {CascadeType.ALL})
@JoinColumn(name = "performancelog_id")
public PerformanceLog getPerformanceLog() {
return performanceLog;
}
这篇关于如何修复 Hibernate 的“对象引用未保存的瞬态实例 - 在刷新之前保存瞬态实例"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!