users@glassfish.java.net

A simple JPA query made difficult with MAX ?

From: <glassfish_at_javadesktop.org>
Date: Thu, 18 Sep 2008 13:27:16 PDT

Dear All,

Having spent all my life utilising Google to answer my questions, I have come across a problem for which I can find no answer and yet it seems so obvious that I can not understand why! If anyone can help, I would appreciate it enormously...

I have an entity Record with a composite primary key consisting of Id and VersionNumber. I need to create a query in JPA which will return the latest Record i.e. highest Id and VersionNumber, in the most elegant way possible.

If this were SQL, I would select max(Id) AS ID, max(versionNumber) AS versionNumber and then group the rest of the fields. I would like to recreate something similar in JPA which returns a simple clean instance (or List) of Record(s).

Can anyone help?

Thanks, John
[Message sent by forum member 'johnnydh' (johnnydh)]

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