"org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing" in EAP 6

Solution Unverified - Updated -

Environment

  • Red Hat JBoss Enterprise Application Platform (EAP) 6
  • Hibernate 4

Issue

The exception below is raised when flushing the session during a transaction:

org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing: org.test.EntityTwo.entityOne -> org.test.EntityOne
        at org.hibernate.engine.spi.CascadingAction$8.noCascade(CascadingAction.java:380)
        at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:176)
        at org.hibernate.event.internal.AbstractSaveEventListener.cascadeBeforeSave(AbstractSaveEventListener.java:424)
        at org.hibernate.event.internal.DefaultPersistEventListener.justCascade(DefaultPersistEventListener.java:190)
        at org.hibernate.event.internal.DefaultPersistEventListener.entityIsPersistent(DefaultPersistEventListener.java:183)
        at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:147)
        at org.hibernate.internal.SessionImpl.firePersistOnFlush(SessionImpl.java:879)
        at org.hibernate.internal.SessionImpl.persistOnFlush(SessionImpl.java:872)
        at org.hibernate.engine.spi.CascadingAction$8.cascade(CascadingAction.java:346)
        at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:399)
        at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:342)
        at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:208)
        at org.hibernate.engine.internal.Cascade.cascadeCollectionElements(Cascade.java:429)
        at org.hibernate.engine.internal.Cascade.cascadeCollection(Cascade.java:369)
        at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:345)
        at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:208)
        at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:165)
        at org.hibernate.event.internal.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:162)
        at org.hibernate.event.internal.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:153)
        at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:89)
        at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
        at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1240)

Resolution

Use one of the following to avoid the exception:

  • Change the cascade setting to ALL or one of the more granular operations such as PERSIST so that the referenced entity will be automatically persisted before the attempt to persist the dependent entity.
  • Explicitly persist the referenced instance before attempting to persist the dependent instance (or flushing the session).

Root Cause

The error message indicates that when attempting to persist an instance of the EntityTwo class, the association entityOne was found to references a transient instance of EntityOne (i.e. one which is not yet persisted to the database). Because the cascade setting for the association is NONE, Hibernate is disallowed from persisting the associated EntityOne and so it cannot persist the EntityTwo instance.

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.

Comments