-->
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: HQL fails on nested query
PostPosted: Thu May 01, 2008 11:18 am 
Newbie

Joined: Fri Apr 04, 2008 2:17 pm
Posts: 15
The following is a query that isn't particularly useful, but it demonstrates what we perceive to be HQL failing. The actual query we want to run is a bit more complex, but when we whittled it down to something very basic it still fails.

The following fails:
Code:
session.createQuery(
   "select P from Product P where 4 > (" +
      "select (" +
         "select count(U) from User U" +
      ")" +
      " + count(PE) from Person PE" +
   ")"
).list()


The following works:
Code:
session.createQuery(
   "select P from Product P where 4 > (" +
      "select (" +
         "1" +
      ")" +
      " + count(PE) from Person PE" +
   ")"
).list()


By themselves these queries return the following:
Code:
session.createQuery("select count(U) from User U").uniqueResult()

returns 1

Code:
session.createQuery("select count(PE) from Person PE").uniqueResult()

returns 1


Our failing query above shows the following in console:
Code:
May 1, 2008 11:16:27 AM org.hibernate.hql.ast.ErrorCounter reportError
SEVERE: <AST>:0:0: unexpected AST node: query
May 1, 2008 11:16:27 AM org.hibernate.hql.ast.ErrorCounter reportError
SEVERE:  right-hand operand of a binary operator was null

With this stack trace
Code:
org.hibernate.hql.ast.QuerySyntaxException: unexpected AST node: query [select P from bpf.database.products.Product P where 4 > (select (select count(U) from bpf.database.userAccounts.User U) + count(PE) from bpf.database.people.Person PE)]
   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.analyze(QueryTranslatorImpl.java:235)
   at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:160)
   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)
   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:597)
   at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301)
   at $Proxy18.createQuery(Unknown Source)
   at bpf.database.products.ProductManager.getLowStock(ProductManager.java:32)
   at bpf.database.products.test.TestProduct.testLowStock(TestProduct.java:156)
   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:597)
   at junit.framework.TestCase.runTest(TestCase.java:168)
   at junit.framework.TestCase.runBare(TestCase.java:134)
   at junit.framework.TestResult$1.protect(TestResult.java:110)
   at junit.framework.TestResult.runProtected(TestResult.java:128)
   at junit.framework.TestResult.run(TestResult.java:113)
   at junit.framework.TestCase.run(TestCase.java:124)
   at junit.framework.TestSuite.runTest(TestSuite.java:232)
   at junit.framework.TestSuite.run(TestSuite.java:227)
   at org.junit.internal.runners.OldTestClassRunner.run(OldTestClassRunner.java:76)
   at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
   at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)



Is this an issue in HQL or are we misunderstanding something about HQL? Has anyone seen a similar problem before?


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 01, 2008 1:46 pm 
Newbie

Joined: Fri Apr 04, 2008 2:17 pm
Posts: 15
Taking this as a sign to create a view instead


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 14, 2008 3:58 pm 
Newbie

Joined: Fri May 25, 2007 6:19 pm
Posts: 2
I just encountered the same problem. Looks like Hibernate cannot apply the results of a select count() as an operand to the multiplication.


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.