persistence@glassfish.java.net

Re: How to reference entity with max value for an attribute

From: Gordon Yorke <gordon.yorke_at_oracle.com>
Date: Tue, 11 Nov 2008 08:37:27 -0500

Something like:
SELECT f FROM Foo f WHERE f.version = (SELECT MAX(f1.version) FROM Foo f1)

--Gordon

Farrukh Najmi wrote:
> Dear colleagues,
>
>
> This is probably a very generic SQL or EJBQL question....
>
> I have a entity class as follows:
>
> @Entity(name = "org.acme.Foo")
> @Table(name = "Foo")
> @Inheritance(strategy = InheritanceType.JOINED)
> public class Foo {
>
> @id
> String id;
>
> @version
> int version;
>
> ...
> }
>
>
> I would like to be able to write a query that reference the latest
> version of Foo as identified by the entity that has the largest value
> for version. Is such a query possible? If so what would it look like.
> Thanks for your help.
>