-->
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.  [ 4 posts ] 
Author Message
 Post subject: HQL doesnt work when using subqueries
PostPosted: Thu Sep 07, 2006 11:02 am 
Newbie

Joined: Mon Aug 07, 2006 2:08 am
Posts: 5
This HQL works when I run it...

select A.pdcid as PDC_ID, A.afterDate as ISSUE_DATE, count(A.pdcid) as NumRecord
from WrkTable A where (A.oneInd = 'B' or A.oneInd is null) and
A.packageId in (select B.packageId from SrcTable B where B.comp_id.fileId = 'file123')
group by A.pdcid, A.afterDate order by count(*)

but when I try to restrict the number of records the query above returns by filtering on one of its alias columns like this(the changes in RED)...

select PDC_ID, ISSUE_DATE, NumRecord from (select A.pdcid as PDC_ID, A.afterDate as ISSUE_DATE, count(A.pdcid) as NumRecord
from WrkTable A where (A.oneInd = 'B' or A.oneInd is null) and
A.packageId in (select B.packageId from SrcTable B where B.comp_id.fileId = 'file123')
group by A.pdcid, A.afterDate order by count(*)) where NumRecord>=2

I get the following error,

org.hibernate.hql.ast.QuerySyntaxError: unexpected token: ( near line 1, column 43 [select PDC_ID, ISSUE_DATE, NumRecord from ( select A.pdcid as PDC_ID, A.afterDate as ISSUE_DATE, count(A.pdcid) as NumRecord from com.smt.sme.pqr.bom.wrk.WrkTable A where (A.oneInd = 'B' or A.oneInd is null) and A.packageId in (select B.packageId from com.smt.sme.pqr.bom.src.SrcTable B where B.comp_id.fileId = 'file123') group by A.pdcid, A.afterDate order by count(*)) where NumRecord>=2]
at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:63)
at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:215)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:127)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:83)
at org.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:425)


oh and for the record I tried this too, but wouldnt work..


select S.PDC_ID, S.ISSUE_DATE, S.NumRecord from (select A.pdcid as PDC_ID, A.afterDate as ISSUE_DATE, count(A.pdcid) as NumRecord
from WrkTable A where (A.oneInd = 'B' or A.oneInd is null) and
A.packageId in (select B.packageId from SrcTable B where B.comp_id.fileId = 'file123')
group by A.pdcid, A.afterDate order by count(*)) S where S.NumRecord>=2


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 07, 2006 12:53 pm 
Senior
Senior

Joined: Wed Aug 17, 2005 12:56 pm
Posts: 136
Location: Erie, PA (USA)
Sorry, but subqueries in FROM clause are not allowed in Hibernate -- only in SELECT & WHERE clauses.

_________________
---- Don't forget to rate! ----


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 14, 2006 4:11 pm 
Newbie

Joined: Mon Aug 07, 2006 2:08 am
Posts: 5
Thanks for providing the solution.....I achieved the filtering by using plain old Java....

tut...tut...I see this inability to add sub-queries in the FROM clause as a big short-coming of Hibernate and of course I have to rewrite that HQL query anways....


Top
 Profile  
 
 Post subject: not allowed in HQL
PostPosted: Thu Sep 14, 2006 5:52 pm 
Beginner
Beginner

Joined: Thu Dec 09, 2004 7:04 pm
Posts: 26
Location: Denver, CO
It's not allowed in HQL, but native SQL works.

If fact, one of our tricks when we can't really get a unique key in hibernate because of outer joins in report queries is to do the following:

Code:
    <sql-query name="myNamedQuery" cacheable="false" callable="false">
      <return alias="stuff" class="hibernate.StuffObject" lock-mode="read" />
        SELECT rownum AS {stuff.id},
               data.* from ( select ....) data
    </sql-query>


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.