SelectionQuery.getResultCount Problem

We have a SelectionQuery that throws an exception when getResultCount() is called - Thing is, The query generated (from hql) not only misses some parts but also alters joins…

When the SelectionQuery’s getResultList() or list() method is called,It generates the following query:

select
    u1_0.id,
    i1_0."id",
    case 
        when i1_2."id" is not null 
            then 4 
    end,
    i1_1.name,
    i1_1.id,
    i1_1.span_end,
    i1_1.span_start,
    i1_0.person_id,
    h1_0.id,
    h1_0.activateToken,
    h1_0.handleable_id,
    h1_0.status,
    h1_0.type 
from
    public."user" u1_0 
join
    public."individualable" u1_1 
        on u1_0.id=u1_1.id 
join
    public."handleable" u1_2 
        on u1_0.id=u1_2.id 
left join
    public."individual" i1_0 
        on i1_0."id"=u1_1."individual_id" 
left join
    public."member" i1_1 
        on i1_0."id"=i1_1.id 
left join
    public."userindividual" i1_2 
        on i1_0."id"=i1_2."id" 
left join
    public."handle" h1_0 
        on u1_2.id=h1_0.handleable_id 
where
    (
        (
            (
                (
                    (
                        i1_1.span_start<=? 
                        and i1_1.span_end is null
                    ) 
                    or (
                        (
                            i1_1.span_start>?
                        ) 
                        and (
                            i1_1.span_start<?
                        )
                    ) 
                    or (
                        (
                            i1_1.span_end>?
                        ) 
                        and (
                            i1_1.span_end<?
                        )
                    ) 
                    or (
                        i1_1.span_start is null 
                        and i1_1.span_end>=?
                    ) 
                    or (
                        i1_1.span_start<=? 
                        and i1_1.span_end>=?
                    )
                )
            )
        )
    ) 
    and (
        h1_0.type=?
    )

And when its getResultCount() is called, It generates the following query:

select
    count(*) 
from
    public."user" u1_0 
join
    public."handleable" u1_2 
        on u1_0.id=u1_2.id 
join
    public."individual" i1_0 
        on i1_0."id"=u1_1."individual_id" 
join
    public."member" i1_1 
        on i1_0."id"=i1_1.id 
join
    public."handle" h1_0 
        on u1_2.id=h1_0.handleable_id 
where
    (
        (
            (
                (
                    (
                        i1_1.span_start<=? 
                        and i1_1.span_end is null
                    ) 
                    or (
                        (
                            i1_1.span_start>?
                        ) 
                        and (
                            i1_1.span_start<?
                        )
                    ) 
                    or (
                        (
                            i1_1.span_end>?
                        ) 
                        and (
                            i1_1.span_end<?
                        )
                    ) 
                    or (
                        i1_1.span_start is null 
                        and i1_1.span_end>=?
                    ) 
                    or (
                        i1_1.span_start<=? 
                        and i1_1.span_end>=?
                    )
                )
            )
        )
    ) 
    and (
        h1_0.type=?
    )

Replacing the selected columns with count(*) is expected BUT its doing more than that - Not only is it not joining all tables BUT also altering join types ANYWAY in the end its referencing a table alias stripped from the statement…

The expected behavior should be just to replace the selected columns with count(*) and nothing more, All the other parts have meaning to the WHERE clause - We’re using hibernate 7.4.7.Final.

Please try to create a reproducer with our test case template and if you are able to reproduce the issue, create a bug ticket in our issue tracker and attach that reproducer.