Quantcast
Channel: Spring Community Forums - Data
Viewing all articles
Browse latest Browse all 297

JPA 2 Criteria API Order by grouping function of OneToMany association

$
0
0
I have the following entity relationship:


@Entity
public class TableA{
private int aid;

private Collection<TableB> bs;

...
}

@Entity
public class TableB{

private int bid;

private Date date1;

private Date date2;

private TableA a;

...
}

I want to list TableA's in the following order:

- max(date1, date2) desc


With native SQL, I could do the following:

SELECT * FROM TableA A, (
SELECT
aid,
MAX(COALESCE(date1, date2, TO_DATE('1901-01-01', 'yyyy-MM-dd'))) AS MAX_DATE
FROM TableB
GROUP BY aid //foreign key
ORDER BY MAX_DATE DESC
) B
WHERE A.aid = B.aid
ORDER BY MAX_DATE DESC;

How can I achieve this using JPA 2 Criteria API?

Viewing all articles
Browse latest Browse all 297

Trending Articles