I'm trying to convert an SQL query that works in mySQL into HQL.
The query returns the rank of a user's max score in a highscore list.
The original query:
Quote:
SELECT count( * ) +1 FROM (
SELECT max( gsd.earnedcoins ) , gsd.character_id
FROM gamesessiondetails gsd, gamesessions gs
WHERE gs.game_id =2
AND gsd.gamesession_id = gs.id
AND gsd.earnedcoins > 123
GROUP BY gsd.character_id
ORDER BY max( gsd.earnedcoins ) DESC , gs.enddate DESC
)
AS rank
the HQL query:
Quote:
select count(*) from (
select max(gsd.earnedCoins), gsd.character from GameSessionDetails gsd, GameSession gs
where (gs.game.id = 2)
and gsd.gameSession = gs
and gsd.earnedCoins > 123
group by gsd.character
order by max(gsd.earnedCoins) desc, gs.endDate desc
) as rank
gives me this exception:
Code:
SEVERE: line 1:22: unexpected token: (
Exception in thread "main" org.hibernate.hql.ast.QuerySyntaxException: unexpected token: ( near line 1, column 22 [select count(*) from (select max(gsd.earnedCoins), gsd.character from net.plazz.atlantis.domain.GameSessionDetails gsd, net.plazz.atlantis.domain.GameSession gs where (gs.game.id = 2) and gsd.gameSession = gs and gsd.gameSession.endDate >= :fromdate and gsd.gameSession.endDate <= :todate and gsd.earnedCoins > 123 group by gsd.character order by max(gsd.earnedCoins) desc, gs.endDate desc ) as rank]
at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:31)
at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:24)
at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:59)
at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:258)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:157)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)
according to the hibernate documentation, subselects should be enclosed in ( and ) . is there an easier way for this query?
Hibernate version:
3.3.1
Name and version of the database you are using:
mySQL 5.0.45
thanks,
thomas