From 91d22f1a090120a9e24dd9e33a462ea85df44303 Mon Sep 17 00:00:00 2001 From: Marco Belladelli Date: Tue, 9 Jan 2024 11:14:16 +0100 Subject: [PATCH] HHH-17615 Fix pruning of soft delete table for joined inheritance --- .../entity/AbstractEntityPersister.java | 4 ++++ .../JoinedSubclassSoftDeleteTests.java | 20 ++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java b/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java index 2f1014830e..a8d6c63ee3 100644 --- a/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java +++ b/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java @@ -3079,6 +3079,10 @@ public abstract class AbstractEntityPersister creationState.getSqlExpressionResolver() ); additionalPredicateCollectorAccess.get().accept( softDeletePredicate ); + if ( tableReference != rootTableReference && creationState.supportsEntityNameUsage() ) { + // Register entity name usage for the hierarchy root table to avoid pruning + creationState.registerEntityNameUsage( tableGroup, EntityNameUse.EXPRESSION, getRootEntityName() ); + } } } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/softdelete/secondary/JoinedSubclassSoftDeleteTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/softdelete/secondary/JoinedSubclassSoftDeleteTests.java index c60632becf..5a54cd0b18 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/softdelete/secondary/JoinedSubclassSoftDeleteTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/softdelete/secondary/JoinedSubclassSoftDeleteTests.java @@ -12,6 +12,7 @@ import java.util.List; import org.hibernate.ObjectNotFoundException; import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.Jira; import org.hibernate.testing.orm.junit.SessionFactory; import org.hibernate.testing.orm.junit.SessionFactoryScope; import org.junit.jupiter.api.AfterEach; @@ -55,11 +56,28 @@ public class JoinedSubclassSoftDeleteTests { scope.inTransaction( (session) -> { // should not return #1 assertThat( session.createQuery( "from JoinedRoot" ).list() ).hasSize( 2 ); + assertThat( session.createQuery( "from JoinedRoot where id = 1" ).list() ).isEmpty(); } ); scope.inTransaction( (session) -> { // should not return #1 - assertThat( session.createQuery( "from JoinedSub" ).list() ).hasSize( 2 ); + assertThat( session.createQuery( "from JoinedSub where id = 1" ).list() ).isEmpty(); + } ); + } + + @Test + @Jira( "https://hibernate.atlassian.net/browse/HHH-17615" ) + void testCountQuery(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { + // should not return #1 + assertThat( session.createQuery( "select count(*) from JoinedRoot" ).uniqueResult() ).isEqualTo( 2L ); + assertThat( session.createQuery( "select count(*) from JoinedRoot where id = 1" ).uniqueResult() ).isEqualTo( 0L ); + } ); + + scope.inTransaction( (session) -> { + // should not return #1 + assertThat( session.createQuery( "select count(*) from JoinedSub" ).uniqueResult() ).isEqualTo( 2L ); + assertThat( session.createQuery( "select count(*) from JoinedSub where id = 1" ).uniqueResult() ).isEqualTo( 0L ); } ); }