Hibernate Exception help: TransientObjectException

后端 未结 4 845
-上瘾入骨i
-上瘾入骨i 2020-12-11 05:37

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

org.hibernate.TransientObjectException: object references an unsaved transient instance - save t

相关标签:
4条回答
  • 2020-12-11 06:13

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

    0 讨论(0)
  • 2020-12-11 06:22

    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.

    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.

    0 讨论(0)
  • 2020-12-11 06:27

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

    0 讨论(0)
  • 2020-12-11 06:34

    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

    0 讨论(0)
提交回复
热议问题