is it possible to count records with coalesce? In Oracle it is possible with this select:
SELECT
COUNT(DISTINCT coalesce(foo.parent, foo.ident)) AS c FROM
FOO foo;
I tried to do with this code:
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<Long> criteriaQuery = criteriaBuilder.createQuery(Long.class);
Root root = criteriaQuery.from(Foo.class);
CriteriaBuilder.Coalesce<Long> coalesce = criteriaBuilder.coalesce();
coalesce.value(root.<Long> get("parent"));
coalesce.value(root.<Long> get("id"));
CriteriaQuery<Long> select = criteriaQuery.select(criteriaBuilder.countDistinct(coalesce));
TypedQuery<Long> typedQuery = entityManager.createQuery(select);
return typedQuery.getSingleResult();
But this throws an exception
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: expecting CLOSE, found '(' near line 1, column 31
Generated query
[select count(distinct coalesce(generatedAlias0.parent, generatedAlias0.id)) from foo.Foo as generatedAlias0]