"org.hibernate.HibernateException: instance was not in a valid state" in Hibernate

Solution Verified - Updated -

Issue

  • Migrating from Hibernate 3 / EAP 5
  • During a transaction an entity referenced by an association is removed/deleted
  • When calling EntityManager.refresh(Object) on the (valid) entity associated with the removed/deleted entity an exception is raised:

    ... org.hibernate.HibernateException: instance was not in a valid state
         at org.hibernate.engine.spi.EntityEntry.isReadOnly(EntityEntry.java:367)
         at org.hibernate.event.internal.DefaultRefreshEventListener.onRefresh(DefaultRefreshEventListener.java:153)
         at org.hibernate.internal.SessionImpl.fireRefresh(SessionImpl.java:1108)
         at org.hibernate.internal.SessionImpl.refresh(SessionImpl.java:1092)
         at org.hibernate.engine.spi.CascadingAction$3.cascade(CascadingAction.java:204)
         at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:396)
         at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:339)
         ...
         at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:164)
         at org.hibernate.event.internal.DefaultRefreshEventListener.onRefresh(DefaultRefreshEventListener.java:122)
         ...
         at org.hibernate.internal.SessionImpl.refresh(SessionImpl.java:1071)
         at org.hibernate.ejb.AbstractEntityManagerImpl.refresh(AbstractEntityManagerImpl.java:942)
         ...
    

Environment

  • Red Hat JBoss Enterprise Application Platform (EAP)
    • 7
    • 6
  • Hibernate
    • 5
    • 4
  • JPA 2.x

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase, tools, and much more.

Current Customers and Partners

Log in for full access

Log In

New to Red Hat?

Learn more about Red Hat subscriptions

Using a Red Hat product through a public cloud?

How to access this content