# createCountQuery - aliased selection and dynamic instantiation error

**URL:** <https://discourse.hibernate.org/t/createcountquery-aliased-selection-and-dynamic-instantiation-error/9529>\
**Category:** Hibernate ORM\
**Created:** [May 17, 2024, 9:27am UTC](https://discourse.hibernate.org/t/createcountquery-aliased-selection-and-dynamic-instantiation-error/9529 "2024-05-17T09:27:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lorenz\_Baier](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/lorenz_baier/32/3141_2.png) [@Lorenz\_Baier](https://discourse.hibernate.org/u/Lorenz_Baier)\
**Post date:** [May 17, 2024, 9:27am UTC](https://discourse.hibernate.org/t/createcountquery-aliased-selection-and-dynamic-instantiation-error/9529/1 "2024-05-17T09:27:40Z")

</div>

when I try using createCountQuery with an HQL which uses a “dynamic” model  
like `SELECT new com.example.model(e.name, e.other)`

At first I get an error saying “aliases are required in CTEs and in subqueries occurring in from clause”  
If I then manually set the aliases on the selection items with:

```auto
if (criteriaQuery.getQueryPart() instanceof SqmQueryPart<?> sqmQueryPart) {
      // hibernate createCountQuery requires aliases on the selection if it creates a subquery internally
      var selection = sqmQueryPart.getFirstQuerySpec().getSelection();
      char c = 'b';
      for (var item : selection.getSelectionItems()) {
        if (item.getAlias() == null) {
          item.alias(Character.toString(++c) + '_');
        }
      }
    }

```

Then I get `java.lang.UnsupportedOperationException: dynamic instantiation in a sub-query is unsupported`

Is this a known issue or is there a known workaround (without having to specify the countQuery manually)?

---

<div class="post-metadata">

**Author:** ![mbladel](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/mbladel/32/2128_2.png) [@mbladel](https://discourse.hibernate.org/u/mbladel)\
**Post date:** [May 20, 2024, 10:50am UTC](https://discourse.hibernate.org/t/createcountquery-aliased-selection-and-dynamic-instantiation-error/9529/2 "2024-05-20T10:50:36Z")

</div>

I don’t think this is an issue, it’s simply a limitation imposed by how the `createCountQuery` API works: it’s a convenience method that, in very simple terms, generates a query like `select count(1) from ( <original_query> )`. Since your original query cointans a dynamic instantiation, you can’t use that directly.

I would suggest either:

- Using `createCountQuery` with a different selection, e.g. selecting the same paths but without the dynamic instantiation itself;
- Creating the count query yourself with HQL to ensure consistent results with your original one.

---

<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:** [May 21, 2024, 7:39am UTC](https://discourse.hibernate.org/t/createcountquery-aliased-selection-and-dynamic-instantiation-error/9529/3 "2024-05-21T07:39:23Z")

</div>

Please create a Jira issue for this and attach a reproducer based on our [test case template](https://github.com/hibernate/hibernate-test-case-templates/blob/master/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java).  
Should be easy to support this.
