persistence@glassfish.java.net

JPA Query Help

From: Dru Devore <ddevore_at_duckhouse.us>
Date: Mon, 17 Nov 2008 09:57:01 -0700

I am working on changing the native queries to JPA type queries and I
found one that I am having problems with.


The native query is similar to this:


SELECT a.*
FROM table1 a, table2 b,
  (SELECT table1_id, max(date) AS max_date
   FROM table2
   GROUP BY table1_id) c
WHERE a.condition1 = true
AND a.id = b.table1_id
AND b.table1_id = c.table1_id
AND b.date = c.max_date
ORDER BY table1_id

table1 and table2 are both entities with table2 being the child table. A
JPA query to get table2 with the first condition above would look like
this:

SELECT table2
FROM table1
JOIN table2
WHERE table1.condition1 = true

I would like to make this into a JPA query but I don't know how to
convert the embedded query.

Basically I am trying to select all the rows out of table2 that have a
max date and have a certain condition in the parent table (table1).

Any suggestions?