Deletion of Parent of @ElementCollection causes a Referential Integrity Constraint Violation

Description

Deleting an entity that contains a `@ElementCollection` with a JPQL query causes a Referential integrity constraint violation.
See reproducer: https://github.com/renegrob/hibernate-test-case-templates/blob/elementcollection-delete-reproducer/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/ElementCollectionDeletionJPAUnitTestCase.java

Activity

Show:

Mark Davis July 6, 2021 at 10:43 AM

I think the HQL DML operations do not guarantee to propagate to associations. But there is indeed a bug with deletion of @ElementCollection if the owning entity is deleted. Here is a quick test case:

 

 

The test fails on the second flush() (line 12) with an error

org.hibernate.exception.ConstraintViolationException: could not execute statement
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:59)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:113)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:99)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:200)
at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:46)
at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3696)
at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3956)
at org.hibernate.action.internal.EntityDeleteAction.execute(EntityDeleteAction.java:123)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:604)
at org.hibernate.engine.spi.ActionQueue.lambda$executeActions$1(ActionQueue.java:478)
at java.util.LinkedHashMap.forEach(LinkedHashMap.java:684)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:475)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:344)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:40)
at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:107)
at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1391)
... 75 more
Caused by: org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: Referential integrity constraint violation: "FKGRST723M48JL71DDXSFAHF1YR: PUBLIC.CONTAINER_COMPONENTS FOREIGN KEY(CONTAINER_ID) REFERENCES PUBLIC.CONTAINER(ID) (1)"; SQL statement:
delete from container where id=? [23503-200]

The executed statements look like this

Hibernate: insert into container (id) values (?)
Hibernate: insert into container_components (container_id, value) values (?, ?)
Hibernate: select container0_.id as id1_2_0_, components1_.container_id as containe1_3_1_, components1_.value as value2_3_1_ from container container0_ left outer join container_components components1_ on container0_.id=components1_.container_id where container0_.id=?
Hibernate: delete from container where id=?
o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 23503, SQLState: 23503
o.h.engine.jdbc.spi.SqlExceptionHelper : Referential integrity constraint violation: "FKGRST723M48JL71DDXSFAHF1YR: PUBLIC.CONTAINER_COMPONENTS FOREIGN KEY(CONTAINER_ID) REFERENCES PUBLIC.CONTAINER(ID) (1)"; SQL statement:
delete from container where id=? [23503-200]

The deletion of components is missing before deletion of the container.

Details

Assignee

Reporter

Worked in

Affects versions

Priority

Created April 7, 2021 at 11:34 AM
Updated July 6, 2021 at 10:43 AM