-->
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.  [ 2 posts ] 
Author Message
 Post subject: org.hibernate.hql.ast.QuerySyntaxError: unexpected token: or
PostPosted: Wed May 19, 2010 8:38 pm 
Newbie

Joined: Fri Apr 30, 2010 5:17 pm
Posts: 13
Can anyone tell me why the query in this statement would give me a QuerySyntaxError? The objection seems to be related to my use of or. All the variables within the text are String variables. I've also included the full exception report.

Code:
Query query = session.createQuery("from employment e where e.jobtitle = " + job +
               " or e.jobtitle = " + job1 + " or e.jobtitle = " + job2 +
               " or e.jobtitle = " + job3 + " or e.jobtitle = " + job4);


Code:
org.hibernate.hql.ast.QuerySyntaxError: unexpected token: or near line 1, column 39 [from employment e where e.jobtitle =  or e.jobtitle = Programmer or e.jobtitle =  or e.jobtitle =  or e.jobtitle = ]
   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:427)
   at org.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:884)
   at org.hibernate.impl.SessionImpl.iterate(SessionImpl.java:920)
   at org.hibernate.impl.QueryImpl.iterate(QueryImpl.java:41)
   at com.ats.dao.EmploymentDAO.getByJob(EmploymentDAO.java:228)
   at com.ats.action.SearchByJob.execute(SearchByJob.java:192)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
   at java.lang.reflect.Method.invoke(Unknown Source)
   at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:440)
   at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:279)
   at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
   at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:163)
   at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
   at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
   at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:249)
   at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
   at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
   at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
   at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)
   at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
   at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
   at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
   at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
   at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
   at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
   at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
   at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:148)
   at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
   at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:93)
   at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
   at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:306)
   at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
   at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)
   at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
   at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:128)
   at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
   at org.apache.struts2.interceptor.ProfilingActivationInterceptor.intercept(ProfilingActivationInterceptor.java:104)
   at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
   at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
   at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
   at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)
   at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
   at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)
   at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
   at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
   at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:148)
   at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
   at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
   at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
   at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:128)
   at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
   at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
   at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
   at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
   at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:468)
   at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
   at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
   at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
   at java.lang.Thread.run(Unknown Source)
Caused by: line 1:39: unexpected token: or
   at org.hibernate.hql.antlr.HqlBaseParser.unaryExpression(HqlBaseParser.java:2813)
   at org.hibernate.hql.antlr.HqlBaseParser.multiplyExpression(HqlBaseParser.java:2687)
   at org.hibernate.hql.antlr.HqlBaseParser.additiveExpression(HqlBaseParser.java:2407)
   at org.hibernate.hql.antlr.HqlBaseParser.concatenation(HqlBaseParser.java:481)
   at org.hibernate.hql.antlr.HqlBaseParser.relationalExpression(HqlBaseParser.java:2195)
   at org.hibernate.hql.antlr.HqlBaseParser.equalityExpression(HqlBaseParser.java:2150)
   at org.hibernate.hql.antlr.HqlBaseParser.negatedExpression(HqlBaseParser.java:2020)
   at org.hibernate.hql.antlr.HqlBaseParser.logicalAndExpression(HqlBaseParser.java:1937)
   at org.hibernate.hql.antlr.HqlBaseParser.logicalOrExpression(HqlBaseParser.java:1901)
   at org.hibernate.hql.antlr.HqlBaseParser.expression(HqlBaseParser.java:1663)
   at org.hibernate.hql.antlr.HqlBaseParser.logicalExpression(HqlBaseParser.java:1834)
   at org.hibernate.hql.antlr.HqlBaseParser.whereClause(HqlBaseParser.java:376)
   at org.hibernate.hql.antlr.HqlBaseParser.queryRule(HqlBaseParser.java:617)
   at org.hibernate.hql.antlr.HqlBaseParser.selectStatement(HqlBaseParser.java:263)
   at org.hibernate.hql.antlr.HqlBaseParser.statement(HqlBaseParser.java:150)
   at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:209)
   ... 72 more


Top
 Profile  
 
 Post subject: Re: org.hibernate.hql.ast.QuerySyntaxError: unexpected token: or
PostPosted: Thu May 20, 2010 3:12 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
All the variables within the text are String variables.


Obviously some of the values are empty strings and the final query is not a valid query. In fact, you are building the query incorrectly and it will never generate a valid HQL since literal strings must be quoted. A better solution which avoids problems if the strings values contains quotes is to use parameter placeholders and use Query.setString() to set the actual values:

Code:
Query query = session.createQuery("from employment e where e.jobtitle = :job or e.jobtitle = :job1 ........");
query.setString("job", job);
query.setString("job1", job1);
// etc.


It would be even easier if you had all parameters in a list or array. Something like this:

Code:
Object[] jobs = new Object[] { job, job1, job2, etc.... };
Query query = session.createQuery("from employment e where e.jobtitle in (:jobs)");
query.setParameterList("jobs", jobs, Hibernate.STRING);


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.