"bonjour à tous" (hi to everyone)
I'm coming from this thread : How can I SELECT rows with MAX(Column value), DISTINCT by another column in SQL?
This guy is doing almost what I need in my project. In fact, I'm trying to do something like Git : retrieve all instances in their last version. Currently, I have a table "UM", with those attributes:
- int id (primary key, auto generated)
- String name (can be duplicated)
- int version (1, 2, 3, [...])
- String fmr
- [others attributes, not used in this case]
I have also this request in SQL (that working well and retrieves the wanted result):
SELECT * FROM agrid.um AS u INNER JOIN
(SELECT u2.name, max(u2.version) AS MaxVersion, u2.fmr
FROM agrid.um AS u2 WHERE u2.fmr = 'CZ2ABVIMG0000' GROUP BY u2.name, u2.fmr) umVersion
ON u.name = umVersion.name AND u.version = umVersion.MaxVersion
WHERE u.fmr = 'ABCDE';
But when I tried to do the same in JPQL, I have some "JOIN" errors:
TypedQuery<UmEty> q = getManager().getEntityManager().createQuery(
"SELECT u FROM UmEty u INNER JOIN"
+ " (SELECT u2.name, max(u2.version) AS MaxVersion, u2.fmr "
+ " FROM UmEty u2 WHERE u2.fmr = :fmr GROUP BY u2.name, u2.fmr) umVersion"
+ " ON u.name = umVersion.name AND u.version = umVersion.MaxVersion WHERE u.fmr = :fmr", UmEty.class);
q.setParameter("fmr", fmr);
return q.getResultList();
With the following exception:
Exception Description: Syntax error parsing
[SELECT u FROM UmEty u INNER JOIN (SELECT u2.name, max(u2.version)
AS MaxVersion, u2.fmr
FROM UmEty u2 WHERE u2.fmr = :fmr GROUP BY u2.name, u2.fmr) umVersion
ON u.name = umVersion.name
AND u.version = umVersion.MaxVersion WHERE u.fmr = :fmr].
[33, 147] The join association path is not a valid expression.
I've also tried to simply use "JOIN" instead of "INNER JOIN", and some other tests (remove the "WHERE" clause, ...), but I always have this exception throwed. I'm not so familiar with JPQL and I've already read the "INNER JOIN" topic on Objectdb (==> https://www.objectdb.com/java/jpa/query/jpql/from ). But even with those information, I'm not able to have a successful request
Can someone tell me why my SQL request translation into a JPQL one is not working ?
Thanks in advance :)