Retrieving the data from the query as collection of pair.
Suppose the query is "Select username, count(id)..". If the query
returns a list String, Long pair then the query actually
returns it as list<Object[]>. So collect the result in a
variable List<Object[]>.
And then iterate over the object array. To get the values,
Cast the corresponding object.
Query q1 = em.createQuery("SELECT e.name, e.salary
FROM Employee e");
List<Object[]> result1 = q1.getResultList();
for (Object[] resultElement : result1) {
String name = (String)resultElement[0];
Double salary = (Double)resultElement[1];
...
}
Query q2 = em.createQuery("SELECT e.name, e.salary,
e.department FROM Employee e");
List<Object[]> result2 = q2.getResultList();
for (Object[] resultElement : result2) {
String name = (String)resultElement[0];
Double salary = (Double)resultElement[1];
Department dept = (Department)resultElement[2];
...
}
Query q3 = em.createQuery("SELECT COUNT(e),
SUM(e.salary) FROM Employee e");
Object[] result3 = (Object[])q3.getSingleResult();
Long count = (Long)result3[0];
Double sum = (Double)result3[1];