jpa - Transform SQL statement into JPQL to get latest entries for each user -
i want transform sql statement
select * (select * `votes` order `date` desc) sub group `username`
into jpql. idea latest vote (order date
desc) each user (group username
).
i understand jpql not support subqueries in from, tried "in"
# not work! select v vote v v.id in (select tmp.id vote tmp order date desc) group v.username
but gives me first entries. tried "max"
# not work! select v vote v v.date in (select max(tmp.date) vote tmp group tmp.username)
as well, gives me multiple entries of same user date
not unique...
edit:
i thought of creating view sql syntax workaround, jpql query refers class (not table) didn't me far ;-)
edit:
then thought of joins , lead me native queries. turns out can use above sql nativequery = true
:
@query(value = "select * (select * `votes` order `date` desc) sub group `username`", nativequery = true)
and works!
see final update: can use initial sql statement if use nativequery = true
- of course code not portable then, native queries bound (current) database type
thanks!
Comments
Post a Comment