-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Subselects on the "from" part of a query
PostPosted: Fri Dec 02, 2005 10:20 pm 
Newbie

Joined: Fri Dec 02, 2005 9:57 pm
Posts: 3
Hibernate version: 3.0.5

Hi. I have a UserSubject class with a date and two many-to-one assotiation, user and subject. I'm trying to select only the latest rows for each subject and one given user, based on the date field.

I've got a query that works on MySQL 4.1 and gives me what I want, but can't port it to HQL.

MySQL query:
Code:
select us.* from user_subject us join
(select user_id, max(data) as data, subject_id
from user_subject group by user_id, subject_id) us2 on
(us.user_id = us2.user_id and us.data = us2.data);


HQL
Code:
        List userSubjectList = getHibernateTemplate().find(
            "select us from UserSubject us, "
            + "(select us2.user.id, max(us2.data) as data, us2.subject.id "
            + "from UserSubject group by us2.user.id, us2.subject.id) us2 "
            + "where (us.user.id = us2.user.id and su.data = su2.data) "
            + "and us.user.id = ? "
            + "order by us.subject.designacao", user.getId());


Hibernate gives me this:
Code:
unexpected token: ( near line 1, column 62 [select us from UserSubject
us, (select us2.user.id, max(us2.data) as data, us2.subject.id from
UserSubject group by us2.user.id, us2.subject.id) us2 where
(us.user.id = us2.user.id and su.data = su2.data) and us.user.id = ?
order by us.subject.designacao]; nested exception is
org.hibernate.hql.ast.QuerySyntaxError: unexpected token:
( near line 1, column 62 [select us from UserSubject us,
(select us2.user.id, max(us2.data) as data, us2.subject.id from
UserSubject group by us2.user.id, us2.subject.id) us2 where
(us.user.id = us2.user.id and su.data = su2.data)
and us.user.id = ? order by us.subject.designacao]


Based on what I have seen, Hibernate only accepts subselects on the where part of the query, is that the case? Is there any way I can rewrite this?

Thanks


Top
 Profile  
 
 Post subject: Hibernates's AST parser bugs
PostPosted: Sat Dec 03, 2005 12:01 am 
Newbie

Joined: Fri Dec 02, 2005 9:57 pm
Posts: 3
Hibernate version: 3.1rc3

Ok, so I've been searching and found out there are some problems with the new Hibernate query parser. I downloaded 3.1rc3 and it has the same problem.

Here is what I got from Hibernate 3.1rc3:
Code:
2005-12-03 03:52:43,868 19467 [http-8080-Processor9] DEBUG org.hibernate.engine.query.QueryPlanCache - unable to locate HQL query plan in cache; generating (select us from UserSubject us join (select us2.user.id, max(us2.data) as data, us2.subject.id from UserSubject group by us2.user.id, us2.subject.id) us2 on (us.user.id = us2.user.id and su.data = su2.data) where us.user.id = ? order by us.subject.designacao)
2005-12-03 03:52:43,875 19474 [http-8080-Processor9] DEBUG org.hibernate.hql.ast.QueryTranslatorImpl - parse() - HQL: select us from pt.esolutions.elearning.model.UserSubject us join (select us2.user.id, max(us2.data) as data, us2.subject.id from pt.esolutions.elearning.model.UserSubject group by us2.user.id, us2.subject.id) us2 on (us.user.id = us2.user.id and su.data = su2.data) where us.user.id = ? order by us.subject.designacao
2005-12-03 03:52:43,901 19500 [http-8080-Processor9] ERROR org.hibernate.hql.PARSER - line 1:66: unexpected token: (
2005-12-03 03:52:43,908 19507 [http-8080-Processor9] DEBUG org.hibernate.hql.ast.ErrorCounter - line 1:66: unexpected token: (
line 1:66: unexpected token: (
   at org.hibernate.hql.antlr.HqlBaseParser.fromJoin(HqlBaseParser.java:1701)
   at org.hibernate.hql.antlr.HqlBaseParser.fromClause(HqlBaseParser.java:1420)
   at org.hibernate.hql.antlr.HqlBaseParser.selectFrom(HqlBaseParser.java:1130)
   at org.hibernate.hql.antlr.HqlBaseParser.queryRule(HqlBaseParser.java:702)
   at org.hibernate.hql.antlr.HqlBaseParser.selectStatement(HqlBaseParser.java:296)
   at org.hibernate.hql.antlr.HqlBaseParser.statement(HqlBaseParser.java:159)
   at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:236)
   at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:153)
   at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:103)
   at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:72)
   at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:52)
   at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:71)
   at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:108)
   at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:88)
   at org.springframework.orm.hibernate3.HibernateTemplate$29.doInHibernate(HibernateTemplate.java:819)
   at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:365)
   at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:817)
   at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:813)
   at pt.esolutions.elearning.dao.hibernate.UserSubjectDAOHibernate.getLatestByUser(UserSubjectDAOHibernate.java:64)
   at pt.esolutions.elearning.bso.impl.UserSubjectBSOImpl.getLatestByUser(UserSubjectBSOImpl.java:62)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:324)
   at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:335)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:181)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:148)
   at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:170)
   at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:176)
   at $Proxy143.getLatestByUser(Unknown Source)
   at pt.esolutions.elearning.action.UserSubjectList.execute(UserSubjectList.java:40)
   at org.springframework.web.struts.DelegatingActionProxy.execute(DelegatingActionProxy.java:106)
   at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
   at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
   at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
   at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
   at pt.esolutions.elearning.util.LoginFilter.doFilter(LoginFilter.java:135)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
   at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:174)
   at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
   at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:75)
   at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
   at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:514)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:868)
   at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:663)
   at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
   at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
   at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
   at java.lang.Thread.run(Thread.java:552)
2005-12-03 03:52:43,915 19514 [http-8080-Processor9] ERROR org.hibernate.hql.PARSER - line 1:87: unexpected token: max
2005-12-03 03:52:43,915 19514 [http-8080-Processor9] DEBUG org.hibernate.hql.ast.ErrorCounter - line 1:87: unexpected token: max
line 1:87: unexpected token: max
   at org.hibernate.hql.antlr.HqlBaseParser.fromRange(HqlBaseParser.java:1584)
   at org.hibernate.hql.antlr.HqlBaseParser.fromClause(HqlBaseParser.java:1428)
   at org.hibernate.hql.antlr.HqlBaseParser.selectFrom(HqlBaseParser.java:1130)
   at org.hibernate.hql.antlr.HqlBaseParser.queryRule(HqlBaseParser.java:702)
   at org.hibernate.hql.antlr.HqlBaseParser.selectStatement(HqlBaseParser.java:296)
   at org.hibernate.hql.antlr.HqlBaseParser.statement(HqlBaseParser.java:159)
   at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:236)
   at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:153)
   at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:103)
   at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:72)
   at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:52)
   at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:71)
   at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:108)
   at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:88)
   at org.springframework.orm.hibernate3.HibernateTemplate$29.doInHibernate(HibernateTemplate.java:819)
   at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:365)
   at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:817)
   at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:813)
   at pt.esolutions.elearning.dao.hibernate.UserSubjectDAOHibernate.getLatestByUser(UserSubjectDAOHibernate.java:64)
   at pt.esolutions.elearning.bso.impl.UserSubjectBSOImpl.getLatestByUser(UserSubjectBSOImpl.java:62)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:324)
   at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:335)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:181)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:148)
   at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:170)
   at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:176)
   at $Proxy143.getLatestByUser(Unknown Source)
   at pt.esolutions.elearning.action.UserSubjectList.execute(UserSubjectList.java:40)
   at org.springframework.web.struts.DelegatingActionProxy.execute(DelegatingActionProxy.java:106)
   at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
   at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
   at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
   at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
   at pt.esolutions.elearning.util.LoginFilter.doFilter(LoginFilter.java:135)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
   at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:174)
   at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
   at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:75)
   at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
   at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:514)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:868)
   at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:663)
   at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
   at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
   at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
   at java.lang.Thread.run(Thread.java:552)


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 03, 2005 8:22 am 
Newbie

Joined: Fri Dec 02, 2005 9:57 pm
Posts: 3
I've posted two JIRA issues:

http://opensource2.atlassian.com/projec ... e/HHH-1219
http://opensource2.atlassian.com/projec ... e/HHH-1220


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.