Thursday, 3 October 2013

SQL to JPA @NamedQuery Conversion

SQL to JPA @NamedQuery Conversion

I'm looking for an intelligent way to convert this SQL statement into
@NamedQuery --if there is a way at all?
SELECT MONTH(dateField), sum(value) FROM mydb.records where status ='paid'
group by MONTH(dateField) order by MONTH(dateField);
I have a JPA @Entity called Record (Hibernate). This details all invoices
in the system that are created on daily basis. There will be many entries
per month. Each record will have a status of paid, overdue, value, and
lots of other info such as name and address of customer and so on etc.
The above statement basically summarises all the data on a month by month
basis and sums the value of all paid invoices per month giveing a summary
of all invoices paid in January, all paid in Feb and so on.....The result
looks somethings like:
datefield value
1 4500
2 5500
3 5669
The only way I can think of doing this using JPA @NamedQuery is to select
all records in the table that are of status 'pai'd and then use my Java
code to do the sorting, ordering and addition in a rather slow and ugly
fashion! Is there a clever way I can do this with @NamedQuery?
Thanks

No comments:

Post a Comment