inheritance - select count query is not working with inheritance

Description

Hi,
There is a bug with Hibernate 5.4.5 and higher. New spring boot 2.2.0 and hibernate 5.4.6 paging is not working properly for repositories. For this part of code in spring boot:



protected <S extends T> TypedQuery<Long> getCountQuery(@Nullable Specification<S> spec, Class<S> domainClass) {
CriteriaBuilder builder = this.em.getCriteriaBuilder();
CriteriaQuery<Long> query = builder.createQuery(Long.class);
Root<S> root = this.applySpecificationToCriteria(spec, domainClass, query);
if (query.isDistinct()) {
query.select(builder.countDistinct(root));
} else {
query.select(builder.count(root));
}

query.orderBy(Collections.emptyList());
return this.em.createQuery(query);
}

 


the count query is not taking into account inherited entities and the query look like below:

 


select count(keycloakfe0_.id) as col_0_0_
from keycloak_fed_mapper_ldap keycloakfe0_
inner join keycloak_federation keycloakfe1_ on keycloakfe0_1_.id_federation=keycloakfe1_.id
where keycloakfe1_.environment=?

 


I've verified all versions since 5.4.0 to 5.4.8. Before version 5.4.5 query was ok. Query for version 5.4.4 is:

 


select count(keycloakfe0_.id) as col_0_0_
from keycloak_fed_mapper_ldap keycloakfe0_
inner join keycloak_fed_mapper keycloakfe0_1_ on keycloakfe0_.id=keycloakfe0_1_.id
inner join keycloak_federation keycloakfe1_ on keycloakfe0_1_.id_federation=keycloakfe1_.id
where keycloakfe1_.environment=?

 


The issue it that this bug has a big impact on spring repositories abstraction with paging mechanism.
I assume it may be related with this feature in 5.4.5 release

https://hibernate.atlassian.net/browse/HHH-12993

If more details required please let me know.

 

Environment

Spring Boot 2.2.0

Status

Assignee

Unassigned

Reporter

Tomasz Bożek

Fix versions

None

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Worked in

5.4.6

Components

Affects versions

Priority

Major
Configure