Using an (aggregate) function in the select clause of a query causes a null pointer exception to be thrown.
This happens when using either the JPA Criteria API or JPQL, and using either a constructor expression or a multi-valued select, and in case of the latter using either a Tuple or an Object return.
The following shows example code with an hsqldb specific function:
Pojo used for ctor expr.
Executing the above JPQL will cause the following exception to be thrown:
A minimal test case is provided here: https://github.com/javaee-samples/javaee7-samples/tree/master/jpa/aggregate-function-in-select
Easiest way to run just that test:
Edit root pom.xml to contain only the JPA module:
Start WildFly 10
From the root of the project: mvn clean test
(note that WildFly is the default profile for the samples project, so -Pwildfly-remote-arquillian can be omitted)
The problem seems to be that internally Hibernate keeps a list of Type instances for every element in the select clause which it matches against the constructor arguments. Only, for the FUNCTION this is Type is always null which eventually causes the NPE to be thrown.
When an Object or Tuple is being returned the same problem occurs that the Type is always null. Even though there's no matching agains constructor arguments in that case, Hibernate still throws a NPE at some point.
DB independent. Tested on Postgres, Derby, H2 and HSQLDB