# SelectionQuery.getResultCount Problem

**URL:** <https://discourse.hibernate.org/t/selectionquery-getresultcount-problem/12434>\
**Category:** Hibernate ORM\
**Created:** [September 21, 2026, 9:26am UTC](https://discourse.hibernate.org/t/selectionquery-getresultcount-problem/12434 "2026-09-21T09:26:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![gmatege](https://avatars.discourse-cdn.com/v4/letter/g/74df32/32.png) [@gmatege](https://discourse.hibernate.org/u/gmatege)\
**Post date:** [September 21, 2026, 9:26am UTC](https://discourse.hibernate.org/t/selectionquery-getresultcount-problem/12434/1 "2026-09-21T09:26:33Z")

</div>

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:

```auto
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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [September 21, 2026, 1:25pm UTC](https://discourse.hibernate.org/t/selectionquery-getresultcount-problem/12434/2 "2026-09-21T13:25:21Z")

</div>

Please try to create a reproducer with our [test case template](https://github.com/hibernate/hibernate-test-case-templates/blob/master/orm/hibernate-orm-7/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java) and if you are able to reproduce the issue, create a bug ticket in our [issue tracker](https://hibernate.atlassian.net) and attach that reproducer.
