The JPA query language supports aggregate functions in the SELECT clause, such as AVG, COUNT, MAX, MIN, SUM, and supports several select_expressions in the SELECT clause, in which case the result is a List the Object ( Object[] ) array. From the JPA specification:
4.8.1 Result SELECT clause type
...
The type of the SELECT result is determined by the result of the select_expressions types contained in it. When multiple select_expressions are used in SELECT, the query result is of type Object[] , and the elements in this result correspond to the order in the order they are specified in the SELECT clause and each of the select_expressions is type by type of result .
In other words, the type of request you mentioned in the comment (and since you did not provide your entity, I will describe my answer to your example), this is not a problem. Here is a sample code:
String qlString = "SELECT AVG(x.price), SUM(x.stocks) FROM Magazine x WHERE ..."; Query q = em.createQuery(qlString); Object[] results = (Object[]) q.getSingleResult(); for (Object object : results) { System.out.println(object); }
References
- JPA 1.0 Specification
- 4.8.1 Result SELECT clause type
- 4.8.4 Aggregate Functions in the SELECT Section
Pascal Thivent Aug 10 '10 at 19:50 2010-08-10 19:50
source share