HHH-14112 : Invalid Pagination COUNT query generated with @Inheritance(strategy = InheritanceType.JOINED)

This commit is contained in:
Gail Badner 2020-07-21 17:30:42 -07:00 committed by Andrea Boriero
parent 16e45bc3d5
commit 148cc15764
2 changed files with 72 additions and 0 deletions

View File

@ -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 );

View File

@ -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;
}
}
}