-->
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.  [ 6 posts ] 
Author Message
 Post subject: Using true and false in HQL
PostPosted: Tue May 15, 2007 11:41 am 
Beginner
Beginner

Joined: Mon Aug 02, 2004 1:08 pm
Posts: 42
Hi everyone.

What is the right way to use boolean constants in HQL?

Consider the following query:

Code:
from MyEntity myEntity where myEntity.name = 'name' and true


It is correct according to the AST grammar. However, Hibernate throws an error.

The following query works but it's not nice:

Code:
from MyEntity myEntity where myEntity.name = 'name' and 1 = 1


This is a minor point but I just wonder if I miss something.

Hibernate version: 3.2.2.ga

Full stack trace of any exception that occurs:


org.hibernate.hql.ast.QuerySyntaxException: unexpected AST node: true near line 1, column 364 [select distinct irecord.id as id from de.disy.preludio2.nokis.record.model.impl.NokisMetadataRecord as irecord where irecord.recordType.name = 'nokisMetadata' and irecord.removed = false and true]
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:585)
at org.springframework.orm.hibernate3.HibernateTemplate$CloseSuppressingInvocationHandler.invoke(HibernateTemplate.java:1197)
at $Proxy16.createQuery(Unknown Source)
at de.disy.commons.hibernate.hql.QueryRule.createQuery(QueryRule.java:228)
at de.disy.commons.hibernate.hql.QueryRule.list(QueryRule.java:263)
at de.disy.commons.hibernate.hql.QueryRule$1.doInHibernate(QueryRule.java:314)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:367)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:333)
at de.disy.commons.hibernate.hql.QueryRule.list(QueryRule.java:310)
at de.disy.preludio2.query.processor.hibernate.AbstractHQLQueryProcessor$HQLRecordDescriptionLister.getRecordDescriptions(AbstractHQLQueryProcessor.java:105)
at de.disy.preludio2.ws.service.csw.impl.AbstractSearchResultsService.createSearchResults(AbstractSearchResultsService.java:36)
at de.disy.preludio2.ws.service.csw.impl.DefaultGetRecordsService.createSearchResults(DefaultGetRecordsService.java:166)
at de.disy.preludio2.ws.service.csw.impl.DefaultGetRecordsService.executeResults(DefaultGetRecordsService.java:149)
at de.disy.preludio2.ws.service.csw.impl.DefaultGetRecordsService.execute(DefaultGetRecordsService.java:83)
at de.disy.preludio2.ws.service.csw.impl.DefaultGetRecordsService.execute(DefaultGetRecordsService.java:1)
at de.disy.preludio2.ws.service.impl.ServiceEndpoint.invokeInternal(ServiceEndpoint.java:23)
at org.springframework.ws.server.endpoint.AbstractMarshallingPayloadEndpoint.invoke(AbstractMarshallingPayloadEndpoint.java:91)
at org.springframework.ws.server.endpoint.MessageEndpointAdapter.invoke(MessageEndpointAdapter.java:35)
at org.springframework.ws.server.MessageDispatcher.dispatch(MessageDispatcher.java:205)
at org.springframework.ws.server.MessageDispatcher.receive(MessageDispatcher.java:166)
at org.springframework.ws.transport.support.WebServiceMessageReceiverObjectSupport.handle(WebServiceMessageReceiverObjectSupport.java:78)
at org.springframework.ws.transport.http.WebServiceMessageReceiverHandlerAdapter.handle(WebServiceMessageReceiverHandlerAdapter.java:60)
at org.springframework.ws.transport.http.MessageDispatcherServlet.doService(MessageDispatcherServlet.java:125)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:399)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:364)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:491)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1074)
at teamdev.jsf.util.ResourceFilter.doFilter(SourceFile:67)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1065)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:365)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:185)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:689)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:391)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:139)
at org.mortbay.jetty.Server.handle(Server.java:285)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:457)
at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:765)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:628)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:209)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:357)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:329)
at org.mortbay.thread.BoundedThreadPool$PoolThread.run(BoundedThreadPool.java:475)


Top
 Profile  
 
 Post subject: Using true and false in HQL
PostPosted: Tue May 15, 2007 1:28 pm 
Beginner
Beginner

Joined: Wed Apr 18, 2007 6:17 pm
Posts: 49
Location: Dominican Republic
hello, when you say true it's a constant or an expression that return true?


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 15, 2007 1:41 pm 
Beginner
Beginner

Joined: Mon Aug 02, 2004 1:08 pm
Posts: 42
I'm not sure how to answer. I guess it's a constant in this case.

What I need is that the query

Code:
from MyEntity myEntity where myEntity.name = 'name' and true


returns all the MyEntity object found.

The query above is literary the query I execute.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 15, 2007 1:54 pm 
Beginner
Beginner

Joined: Wed Apr 18, 2007 6:17 pm
Posts: 49
Location: Dominican Republic
i believe that you could remove the true constant and it will work just fine, if not, you must provide more information to reproduce your scenario,

regards


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 15, 2007 2:07 pm 
Beginner
Beginner

Joined: Mon Aug 02, 2004 1:08 pm
Posts: 42
ataveras wrote:
i believe that you could remove the true constant and it will work just fine, if not, you must provide more information to reproduce your scenario


Well, OF COURSE I could remove "and true" from this very query and it will work. But this is not the question I'm asking.

What I am asking is which is the right syntax to use for boolean constants in logical expressions?

I wonder why
Code:
true
or
Code:
false
produce QuerySyntaxException? They do not break the HQL grammar and it's quite logical that they work. Isn't it reasonable for a query:

Code:
from MyEntity where true


to work?


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 15, 2007 2:59 pm 
Beginner
Beginner

Joined: Wed Apr 18, 2007 6:17 pm
Posts: 49
Location: Dominican Republic
i understand your point now, and it's true i went a little further with your example and wrote this and didn't work either,


Code:
from Table t where t.booleanField


And it gave me a QuerySyntaxException, i think that the QueryParser could be more "clever" and parse it as correct.


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