如何修复休眠的“对象引用未保存的瞬态实例-在刷新之前保存瞬态实例".错误 [英] How to fix the Hibernate "object references an unsaved transient instance - save the transient instance before flushing" error

查看:113
本文介绍了如何修复休眠的“对象引用未保存的瞬态实例-在刷新之前保存瞬态实例".错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Hibernate保存对象时收到以下错误

I receive following error when I save the object using Hibernate

object references an unsaved transient instance - save the transient instance before flushing

推荐答案

您应该在集合映射中包含cascade="all"(如果使用xml)或cascade=CascadeType.ALL(如果使用注释).

You should include cascade="all" (if using xml) or cascade=CascadeType.ALL (if using annotations) on your collection mapping.

之所以发生这种情况,是因为您的实体中有一个集合,并且该集合具有一个或多个数据库中不存在的项.通过指定上述选项,您可以告诉hibernate在保存其父项时将它们保存到数据库中.

This happens because you have a collection in your entity, and that collection has one or more items which are not present in the database. By specifying the above options you tell hibernate to save them to the database when saving their parent.

这篇关于如何修复休眠的“对象引用未保存的瞬态实例-在刷新之前保存瞬态实例".错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆