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:
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)?