We're updating the issue view to help you get more done. 

Persist of detached entity in derived ID

Description

When a @ManyToOne relation is present on a composite key, Hibernate tries to persist that object even if no persist operation is done on the parent entity. This cause PersistentObjectException when an entity is retrieved from cache and Hibernate tries to persist its detached @ManyToOne key.

For example:

1 2 3 4 5 6 7 8 9 10 11 12 @Entity @IdClass(MyBeanId.class) public class MyBean { @Id @ManyToOne private MyOtherBean other; @Id private Long id; }

A first query.getSingleResult() works, but all subsequent ones throw this:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 Caused by: org.hibernate.PersistentObjectException: detached entity passed to persist: MyOtherBean at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:124) at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:58) at org.hibernate.tuple.entity.AbstractEntityTuplizer$IncrediblySillyJpaMapsIdMappedIdentifierValueMarshaller.getIdentifier(AbstractEntityTuplizer.java:384) at org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:219) at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:4633) at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:4344) at org.hibernate.engine.internal.ForeignKeys.isTransient(ForeignKeys.java:226) at org.hibernate.engine.internal.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:276) at org.hibernate.type.EntityType.getIdentifier(EntityType.java:455) at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:153) at org.hibernate.param.NamedParameterSpecification.bind(NamedParameterSpecification.java:52) at org.hibernate.loader.hql.QueryLoader.bindParameterValues(QueryLoader.java:627) at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1943) at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1896) at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1874) at org.hibernate.loader.Loader.doQuery(Loader.java:919) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:336) at org.hibernate.loader.Loader.doList(Loader.java:2610) at org.hibernate.loader.Loader.doList(Loader.java:2593) at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2422) at org.hibernate.loader.Loader.list(Loader.java:2417) at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:501) at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:371) at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:216) at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1339) at org.hibernate.internal.QueryImpl.list(QueryImpl.java:87) at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:606) at org.hibernate.jpa.internal.QueryImpl.getSingleResult(QueryImpl.java:529) ... 122 more

Environment

WildFly 10.0

Status

Assignee

Gail Badner

Reporter

Giovanni Lovato

Fix versions

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Affects versions

5.1.0
4.2.20

Priority

Blocker