1

I'm trying to get the latest entry for each group and I got a query from this post

GROUP BY with MAX(DATE)

and here is the query

SELECT t1
FROM TrainTable t1 LEFT JOIN TrainTable t2
ON (t1.Train = t2.Train AND t1.Time < t2.Time)
WHERE t2.Time IS NULL;

When I try to implement the same query in Spring data JPA @Query, I'm getting

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Path expected for join! [SELECT t1
FROM TrainTable t1 LEFT JOIN TrainTable t2
ON (t1.Train = t2.Train AND t1.Time < t2.Time)
WHERE t2.Time IS NULL]

Is there any possible way to use the same table entity in left join?

Gajanan Kulkarni
  • 697
  • 6
  • 22
Nagendran
  • 11
  • 2
  • Show your @Query annotation. Are you setting the `nativeQuery` to `true`? `@Query(value = "", nativeQuery = true)` – jannis Nov 21 '16 at 11:15
  • This is my @Query("SELECT t1 FROM TrainTable t1 LEFT JOIN TrainTable t2 ON (t1.Train = t2.Train AND t1.Time < t2.Time) WHERE t2.Time IS NULL"). No I'm not setting nativeQuery as true – Nagendran Nov 21 '16 at 11:26
  • So what happens if you set it? Without setting this flag the query is treated as a JPQL, not SQL – jannis Nov 21 '16 at 11:45
  • I will get it a try now, but I would like to know, is it possible to handle this problem in JPQL? thanks – Nagendran Nov 21 '16 at 11:53
  • Asnwers like [this one](http://stackoverflow.com/a/9570954/4494577) make me think it is possible. – jannis Nov 21 '16 at 11:58

0 Answers0