4
votes

I am getting error as unexpected token :( below the query, but this same query is working fine in SQL

"SELECT slh.assetId,slh.mapHierarchy "
+ "FROM AssetHistory slh "
+ "INNER JOIN "
+ "(SELECT assetId, MAX(lastUpdated) AS MaxDateTime "
+ "FROM AssetHistory "
+ "GROUP BY assetId) slh2 "
+ "ON slh.assetId = slh2.assetId "
+ "AND slh.lastUpdated =slh2.MaxDateTime ";

I hope somebody can help me.

Here is the error log:

Jan 25, 2016 4:04:32 PM org.hibernate.hql.internal.ast.ErrorCounter reportError ERROR: line 1:89: unexpected token: ( Jan 25, 2016 4:04:32 PM org.hibernate.hql.internal.ast.ErrorCounter reportError ERROR: line 1:89: unexpected token: ( line 1:89: unexpected token: ( at org.hibernate.hql.internal.antlr.HqlBaseParser.fromJoin(HqlBaseParser.java:1661) at org.hibernate.hql.internal.antlr.HqlBaseParser.fromClause(HqlBaseParser.java:1380) at org.hibernate.hql.internal.antlr.HqlBaseParser.selectFrom(HqlBaseParser.java:1086) at org.hibernate.hql.internal.antlr.HqlBaseParser.queryRule(HqlBaseParser.java:731) at org.hibernate.hql.internal.antlr.HqlBaseParser.selectStatement(HqlBaseParser.java:323) at org.hibernate.hql.internal.antlr.HqlBaseParser.statement(HqlBaseParser.java:186) at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:295) at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:203) at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158) at org.hibernate.engine.query.spi.HQLQueryPlan.(HQLQueryPlan.java:126) at org.hibernate.engine.query.spi.HQLQueryPlan.(HQLQueryPlan.java:88) at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:167)

and

ERROR: line 1:106: unexpected token: max line 1:106:unexpected token: max at org.hibernate.hql.internal.antlr.HqlBaseParser.fromRange(HqlBaseParser.java:1544) at org.hibernate.hql.internal.antlr.HqlBaseParser.fromClause(HqlBaseParser.java:1388) at org.hibernate.hql.internal.antlr.HqlBaseParser.selectFrom(HqlBaseParser.java:1086) at org.hibernate.hql.internal.antlr.HqlBaseParser.queryRule(HqlBaseParser.java:731) at org.hibernate.hql.internal.antlr.HqlBaseParser.selectStatement(HqlBaseParser.java:323) at org.hibernate.hql.internal.antlr.HqlBaseParser.statement(HqlBaseParser.java:186) at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:295) at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:203) at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158) at org.hibernate.engine.query.spi.HQLQueryPlan.(HQLQueryPlan.java:126) at org.hibernate.engine.query.spi.HQLQueryPlan.(HQLQueryPlan.java:88) at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:167) at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301) at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236) at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1800)

2

2 Answers

3
votes

I think you should move subquery from join to where clause.

"SELECT slh.assetId,slh.mapHierarchy "
+ "FROM AssetHistory slh "
+" WHERE (slh.assetId, slh.lastUpdated)"
+ " IN 
+ "(SELECT assetId, MAX(lastUpdated) AS MaxDateTime "
+ "FROM AssetHistory "
+ "GROUP BY assetId);
3
votes

I think HQL does not support subqueries in FROM clause.

14.13. Subqueries

Note that HQL subqueries can occur only in the select or where clauses.