users@glassfish.java.net

Re: A simple JPA query made difficult with MAX ?

From: <glassfish_at_javadesktop.org>
Date: Fri, 19 Sep 2008 06:59:09 PDT

Hello John,

One way might be to use a seperate query to return the max values and then find the Records matching those values. This can be done in a subquery such as:
  "SELECT record FROM Record record WHERE Id = (SELECT max(Id) FROM Record )"
Of course, this should return records with only the max ID. You could order them by versionNumber and then only take the first Record returned:
  query = em.createQuery("SELECT record FROM Record record WHERE Id = (SELECT max(Id) FROM Record ) ORDER BY record.versionNumber"
  query.setMaxRows(1);

There are probably many ways to do the same thing though.

Best Regards,
Chris
[Message sent by forum member 'chris_delahunt' (chris_delahunt)]

http://forums.java.net/jive/thread.jspa?messageID=300387