JPA 2.0 Criteria Builder nested function in aggregate function gives QuerySyntaxException

Viewed 542

I have a Postgres database project that uses JSONB columns to store some arbitrary data, I've already registered the jsonb_extract_path_text function. What I wish to achieve is write a JPA Criteria Query that will count the occurrences of some specific JSON keys, it's SQL equivalent is:

test=# select * from domain
test-# ;
 id  |     extra      
-----+----------------
   1 | {"test1": "a"}
  51 | {"test1": "b"}
 101 | {"test2": "b"}
 151 | {"test2": "e"}
 201 | {"test2": "f"}
(5 rows)

test=# select count(jsonb_extract_path_text(extra, 'test1')) from domain;
 count 
-------
     2
(1 row)

Now with the following Criteria Query

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery cq = cb.createQuery();
From from = cq.from(Domain.class);
cq.select(cb.count(cb.function("jsonb_extract_path_text", String.class, from.get("extra"), cb.literal("test1"))));
TypedQuery query = em.createQuery(cq);
List list = query.getResultList();

I get the following exception

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: expecting CLOSE, found '(' near line 1, column 22 [select count(function('jsonb_extract_path_text', generatedAlias0.extra, 'test1')) from com.example.demo.Domain as generatedAlias0]
    at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:74) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.hql.internal.ast.ErrorTracker.throwQueryException(ErrorTracker.java:93) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:297) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:189) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:144) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:113) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:73) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:155) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:604) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:716) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    ... 43 common frames omitted

I'm not sure if JPA Specifications supports such an approach , nesting a custom function within an aggregate function or if that is actually a bug in Hibernate.

If that is not actually supported by the JPA standards, what other options I have in order to achieve that approach ? I've already got it working with Hibernate Criteria and Projections.sqlProjection function, but as far as I know, this is deprecated.

Edit 1: I did actually try the same implementation using EclipseLink, and it was working properly, so I'm more inclined to suspect that this is a bug with Hibernate JPA implementation itself.

0 Answers
Related