Until now I selected Entities like this:
public List<Customer> findAll()
{
TypedQuery<Customer> query = getEntityManager().createNamedQuery(Customer.FindAll, Customer.class);
return query.getResultList();
}
This works. And with method invocation of those Entities from the result set, I can even get the data from “joined” tables, so I never need to make a “real” join in JPQL. (This is what JPA is about).
Now I’d like to get a list of customers with their turnover within a given period, let’s say January 2011 until June 2012. A native query will look like this:
select cus_customer_code, cus_last_name, sum(trn_total_turnover_euro)
from customer, transaction
where t2c_cus_id = cus_id
and date_part('month', trn_date) between 1 and 6
and date_part('year', trn_date) between 2011 and 2012
group by cus_customer_code, cus_last_name;
This query works in database. But how can I do this in JPQL and what would be the result of this statement, a list of a pair of 2 Entities or something like that? Or is this not possible in this way in JPQL, so do I have to use native queries instead?
Create an additional class with members you want. In this example,
org.test.MyClass.Then use the
Or, for scalar values:
Hibernate didn’t implement scalars properly until recently:
https://hibernate.onjira.com/browse/HHH-6591
https://hibernate.onjira.com/browse/HHH-6695
Here’s a nice article about
SELECTs in general:http://www.objectdb.com/java/jpa/query/jpql/select
Hibernate manual has it covered quite briefly: http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html_single/#queryhql-select