From 148cc1576477470b57bcee934458234b60a8a8d1 Mon Sep 17 00:00:00 2001 From: Gail Badner Date: Tue, 21 Jul 2020 17:30:42 -0700 Subject: [PATCH] HHH-14112 : Invalid Pagination COUNT query generated with @Inheritance(strategy = InheritanceType.JOINED) --- .../hql/internal/ast/util/JoinProcessor.java | 8 +++ .../query/hhh14112/HHH14112Test.java | 64 +++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/query/hhh14112/HHH14112Test.java diff --git a/hibernate-core/src/main/java/org/hibernate/hql/internal/ast/util/JoinProcessor.java b/hibernate-core/src/main/java/org/hibernate/hql/internal/ast/util/JoinProcessor.java index 9beab0993a..bc7d3cb3d9 100644 --- a/hibernate-core/src/main/java/org/hibernate/hql/internal/ast/util/JoinProcessor.java +++ b/hibernate-core/src/main/java/org/hibernate/hql/internal/ast/util/JoinProcessor.java @@ -142,6 +142,14 @@ public class JoinProcessor implements SqlTokenTypes { if ( role != null ) { result.add( fromElement.getOrigin().getPropertyTableName(role.substring(role.lastIndexOf('.') + 1)) ); } + final EntityPersister entityPersister = fromElement.getEntityPersister(); + if ( entityPersister instanceof AbstractEntityPersister ) { + AbstractEntityPersister aep = (AbstractEntityPersister) entityPersister; + while ( !aep.filterFragment( "", Collections.emptyMap() ).isEmpty() && aep.getMappedSuperclass() != null ) { + Collections.addAll( result, aep.getTableNames() ); + aep = (AbstractEntityPersister) walker.getSessionFactoryHelper().findEntityPersisterByName( aep.getMappedSuperclass() ); + } + } AST withClauseAst = fromElement.getWithClauseAst(); if ( withClauseAst != null ) { collectReferencedTables( new ASTIterator( withClauseAst ), result ); diff --git a/hibernate-core/src/test/java/org/hibernate/query/hhh14112/HHH14112Test.java b/hibernate-core/src/test/java/org/hibernate/query/hhh14112/HHH14112Test.java new file mode 100644 index 0000000000..480be66618 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/query/hhh14112/HHH14112Test.java @@ -0,0 +1,64 @@ +package org.hibernate.query.hhh14112; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; + +import org.hibernate.annotations.Where; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.junit.Test; + +import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; + +/** + * @author Ganesh Tiwari + * @author Nathan Xu + */ +@TestForIssue(jiraKey = "HHH-14112") +public class HHH14112Test extends BaseCoreFunctionalTestCase { + + @Test + public void testCountSubObjectNotThrownExceptionBecauseOfWhere() { + doInJPA(this::sessionFactory, em -> { + em.createQuery( "SELECT count(*) FROM SubObject", Long.class).getSingleResult(); + }); + } + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { HHH14112Test.Super.class, HHH14112Test.SubObject.class }; + } + + @Entity(name = "Super") + @Inheritance(strategy = InheritanceType.JOINED) + @Where(clause = "DELETED = 0") + public static class Super { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + public Long id; + + public boolean deleted; + } + + @Entity(name = "SubObject") + public static class SubObject extends Super { + + public String name; + + public int age; + + public SubObject() { + } + + public SubObject(String name, int age) { + this.name = name; + this.age = age; + } + + } +}