Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
151 views
in Technique[技术] by (71.8m points)

java - Hibernate Exception help: TransientObjectException

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: ......

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???

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

  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.

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...