I got the following SQL which I wanted to transform into valid HQL. The issue with this is, that its not permitted to join on a sub-query per the docs. Even though these are old docs (v3.3) this section still seems to still hold in hibernate 5.3.
SELECT f.date,
f.name,
SUM(f.seats)
FROM Foo f
INNER JOIN (SELECT fl.date,
fl.start + fl.end AS code
FROM Foo fl
WHERE fl.date >= (SELECT MAX(earliestDate)
FROM Bar)
AND fl.name = :name) fl
ON f.start + f.end = code
AND f.date = fl.date
WHERE f.date >= ( SELECT MAX(earliestDate)
FROM Bar)
GROUP BY f.date,
f.name
ORDER BY f.date ASC,
SUM(f.seats) DESC
I came up with this HQL:
SELECT NEW com.company.project.model.FooRepresentation( f.fooId.date,
f.fooId.name,
SUM(f.seats))
FROM Foo f
INNER JOIN (SELECT fl.fooId.date,
fl.fooId.start + fl.fooId.end AS code
FROM Foo fl
WHERE fl.fooId.date >= (SELECT MAX(earliestDate)
FROM FooConfig)
AND fl.fooId.name = :name) fl
ON f.fooId.start + f.fooId.end = code
AND f.fooId.date = fl.fooId.date
WHERE f.fooId.date >= ( SELECT MAX(earliestDate)
FROM FooConfig)
GROUP BY f.fooId.date,
f.fooId.name
ORDER BY f.fooId.date ASC,
SUM(f.seats) DESC
Trying to execute this HQL query, results in this exception
org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: ( near line 1, column 169
which hints to the (
at INNER JOIN (SELECT
.
Is there a way to join on sub-queries within HQL? If not, what would be the best way to achieve the same results as the SQL with HQL?
I am not that good with converting the inner join on <sub-query>
to an actual where
clause where the sub-query is permitted
@Entity
class (NEW com.company.project.model.FooRepresentation()
) – XtremeBaumerNEW com.company.project.model.FooRepresentation
syntax? But I mean, that's one line of code to map from theObject[]
to a constructor call, compared to rewriting it all :) Anyway - I'm not sure this will be an easy rewrite... – Lukas Eder