Java Hibernate 异常帮助:TransientObjectException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1044913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:40:29  来源:igfitidea点击:

Hibernate Exception help: TransientObjectException

javahibernate

提问by Rafael

I am getting the following exception when I try to update an object:

当我尝试更新对象时出现以下异常:

org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: ......

org.hibernate.TransientObjectException: 对象引用了一个未保存的瞬态实例 - 在刷新之前保存瞬态实例:......

Can anyone help???

有人可以帮忙吗???

The object that I am trying to update has the 'lazy' attribute set to false in the mapping file. Seems like hibernate is expecting me to save child objects before it flushes the update???

我尝试更新的对象在映射文件中将 'lazy' 属性设置为 false。似乎 hibernate 期望我在刷新更新之前保存子对象???

EDIT (ADDED):

编辑(添加):

<hibernate-mapping>
    <class name="utils.message.Message" table="messages">
        <id name="id" column="message_id">
            <generator class="native" />
        </id>
        <property name="message_text" column="message_text" />
        <property name="message_file" column="message_file" />
        <property name="is_active" column="is_active" type="boolean"/>
        <property name="is_global" column="is_global" type="boolean"/>
        <property name="start" column="start" type="java.util.Date"/>
        <property name="end" column="end" type="java.util.Date"/>
        <property name="last_updated" column="last_updated" type="java.util.Date"/>     

        <many-to-one name="last_updated_by" class="utils.user.User" column="last_updated_by" lazy="false"/>
        <many-to-one name="healthDepartment" class="utils.healthdepartment.HealthDepartment" column="health_department_id" lazy="false"/>

    </class>
</hibernate-mapping>

采纳答案by Rafael

App is in a Spring environment. Fix: to run update from within Hibernate environment.

应用程序在 Spring 环境中。修复:从休眠环境中运行更新。

回答by cliff.meyers

TransientObjectException occurs when you save an object which references another object that is transient (meaning it has the "default" identifier value, frequently null) and then flush the Session. This commonly happens when you are creating an entire graph of new objects but haven't explicitly saved all of them. There are two ways to work around this:

当您保存引用另一个瞬态对象的对象(意味着它具有“默认”标识符值,通常为空)然后刷新会话时,会发生 TransientObjectException。当您创建新对象的整个图形但尚未明确保存所有对象时,通常会发生这种情况。有两种方法可以解决这个问题:

  1. As you suggest, you could use cascading of saves to other associated objects. However, cascading wasn't really intended as a workaround for TOE but rather as a convenience for saving a group of related objects that are frequently manipulated together. If you detach your objects without its full set of associated data and then save it with cascading enabled, you could inadvertently delete data you don't want to lose.
  2. Ensure that all transient objects in your graph are explicitly saved as part of your unit of work. This is really just a case of understanding how your application will be creating an object graph and what entities are transient and which might be persistent or detached.
  1. 正如您所建议的,您可以使用级联保存到其他关联对象。然而,级联实际上并不是作为 TOE 的一种解决方法,而是为了方便保存一组经常一起操作的相关对象。如果您在没有完整关联数据集的情况下分离您的对象,然后在启用级联的情况下保存它,您可能会无意中删除您不想丢失的数据。
  2. 确保图形中的所有瞬态对象都明确保存为工作单元的一部分。这实际上只是了解您的应用程序将如何创建对象图以及哪些实体是瞬态的,哪些可能是持久的或分离的。

I would recommend reading this entire chapter from the Hibernate docs to understand fully the terminology of transient, persistent and detached:

我建议阅读 Hibernate 文档中的这一整章,以完全理解瞬态、持久和分离的术语:

http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/objectstate.html

http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/objectstate.html

回答by skaffman

With a many-to-one relationship, it would not be appropriate for Hibernate to cascade persistence operations, since the "one" is conceptually an entity shared between many others. This kind of relationship isn't a "child object", as you put it.

对于多对一关系,Hibernate 不适合级联持久性操作,因为“一个”在概念上是许多其他实体之间共享的实体。正如您所说,这种关系不是“子对象”。

You can override this behaviour by explicitly setting the cascade attribute on the relation, or you can manually persist the other end of the many-to-one relations.

您可以通过在关系上显式设置级联属性来覆盖此行为,或者您可以手动保留多对一关系的另一端。

回答by vishu

dude use property "cascade = true"in mapping. all will well... V-Boy

老兄在映射中使用属性“cascade = true”。一切都会好的... V-Boy