I am new to JPA. So my question should be so simple to some.
Below is the Simple Query in SQL which i would like to convert to JPA. I already have an entity class called TimeEnt
.
SELECT SUM(TimeEntryActualHours) as UnBilledHrs, SUM (TimeEntryAmount) as UnbilledAmount FROM TimeEnt WHERE MatterID = 200
SUM. The aggregate function SUM() is used to calculate the sum of all the values of the select column. It returns the sum of values in a set.
JPQL supports the five aggregate functions of SQL: COUNT - returns a long value representing the number of elements. SUM - returns the sum of numeric values. AVG - returns the average of numeric values as a double value.
There is no standard way to get JPA to return a map. Iterating manually should be fine. The time to iterate a list/map in memory is going to be small relative to the time to execute/return the query results.
An aggregate function in SQL performs a calculation on multiple values and returns a single value. SQL provides many aggregate functions that include avg, count, sum, min, max, etc. An aggregate function ignores NULL values when it performs the calculation, except for the count function.
The JPA Query Language does support aggregates functions in the SELECT clause like AVG, COUNT, MAX, MIN, SUM and does support multiple select_expressions in the SELECT clause, in which case the result is a List
of Object
array (Object[]
). From the JPA specification:
4.8.1 Result Type of the SELECT Clause
...
The result type of the SELECT clause is defined by the the result types of the select_expressions contained in it. When multiple select_expressions are used in the SELECT clause, the result of the query is of type
Object[]
, and the elements in this result correspond in order to the order of their specification in the SELECT clause and in type to the result types of each of the select_expressions.
In other words, the kind of query you mentioned in a comment (and since you didn't provide your entity, I'll base my answer on your example) is supported, no problem. Here is a code sample:
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); }
Lets think we have entity called Product
:
final Query sumQuery = entityManager .createQuery("SELECT SUM(p.price), SUM(p.sale) FROM Product p WHERE p.item=:ITEM AND ...."); sumQuery.setParameter("ITEM","t1"); final Object result= sumQuery.getSingleResult(); // Return an array Object with 2 elements, 1st is sum(price) and 2nd is sum(sale). //If you have multiple rows; final Query sumQuery = entityManager .createQuery("SELECT SUM(p.price), SUM(p.sale) FROM Product p WHERE p.item in (" + itemlist + ") AND ...."); // Return a list of arrays, where each array correspond to 1 item (row) in resultset. final List<IEniqDBEntity> sumEntityList = sumQuery.getResultList();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With