Bug appeared after Spring Boot version 3.4. With version 3.3.5 this example works fine.

There is problem with count query when filtering data using specification and pagination. Entity employee and person are connected with strategy = InheritanceType.JOINED.

Reproducer example is provided in: specificationCountQueryBug.zip

Provided example has EmployeeSpecifications.java where specification is written so that causes bug.

This is example of COUNT generated SQL query which fails because alias for table used in WHERE part of statement is not present in JOIN. Same query for records retrieval with all columns is generated fine.

select
        distinct count(distinct e1_0.id)  
    from
        employee e1_0 
    left join
        customer c1_0 
            on e1_0.id=c1_0.employee_id 
    left join
        region r1_0 
            on r1_0.id=c1_0.region_id 
    left join
        country c2_0 
            on c2_0.id=r1_0.country_id 
    where
        lower(e1_1.firstname) like lower('%Alice%') escape '' 
        or lower(e1_1.lastname) like lower('%Alice%') escape '' 
        or lower(e1_0.email) like lower('%Alice%') escape '' 
        or lower(c2_0.name) like lower('%Alice%') escape '' 
        or lower(r1_0.name) like lower('%Alice%') escape ''`
> org.springframework.dao.InvalidDataAccessResourceUsageException: could not prepare statement [Column "E1_1.FIRSTNAME" not found; SQL statement:

Not sure if this problem is maybe Hibernate related.

Comment From: philwebb

I'm not sure if this is a Hibernate or Spring Data JPA issue, but it's unlikely to be a direct Spring Boot issue. I think raising this at https://github.com/spring-projects/spring-data-jpa/issues would be a good start since that team is more likely to have the required expertise to diagnose the issue.