-->
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.  [ 7 posts ] 
Author Message
 Post subject: Attribute names conflicting with keywords in HQL queries?
PostPosted: Thu Nov 24, 2005 3:19 am 
Newbie

Joined: Mon Nov 29, 2004 10:05 am
Posts: 5
I am having trouble running an HQL query in which i refer to the right property of one of my persistent beans. Apparently, "right" is an HQL keyword, which i guess is the reason why my query won't run (details below), and besides, the same query works fine if i rename the right property of my bean into something else.

I would like to avoid renaming my bean property, thus i'd like to know if there is a way (syntax?) to tell the HQL interpreter that the right i'm using is not the keyword.

Details:

I have a class named Mandate with a property named right which returns an object of type Right. I am performing the following HQL query (i am using Hibernate 3.0.5):

Code:
List mandates = session.createQuery (
  "from " + Mandate.class.getName() + " where right is not null").list();


Attempting to run this query results in the following exception:

Code:
SEVERE: *** ERROR: line 1:38: unexpected token: right
org.hibernate.hql.ast.QuerySyntaxError: unexpected token: right near line 1, column 38 [from foo.bar.Mandate where right is not null]
   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.list(SessionImpl.java:834)
   at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)
   at be.smalsmvm.magma.dao.SearchMandatesDemo.main(SearchMandatesDemo.java:41)
Caused by: line 1:38: unexpected token: right
   at org.hibernate.hql.antlr.HqlBaseParser.negatedExpression(HqlBaseParser.java:2032)
   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)
   ... 7 more


Interestingly, this won't work either when i try to remove any ambiguity by aliasing the Mandate class and prefixing the right property:

Code:
List mandates = session.createQuery (
  "from " + Mandate.class.getName() + " as mandate where mandate.right is not null").list();


Code:
SEVERE: *** ERROR: line 1:46: unexpected token: .
org.hibernate.hql.ast.QuerySyntaxError: unexpected token: . near line 1, column 46 [from foo.bar.Mandate as mandate where mandate.right is not null]
   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.list(SessionImpl.java:834)
   at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)
   at be.smalsmvm.magma.dao.SearchMandatesDemo.main(SearchMandatesDemo.java:40)
Caused by: line 1:46: unexpected token: .
   at org.hibernate.hql.antlr.HqlBaseParser.primaryExpression(HqlBaseParser.java:3327)
   at org.hibernate.hql.antlr.HqlBaseParser.atom(HqlBaseParser.java:3031)
   at org.hibernate.hql.antlr.HqlBaseParser.unaryExpression(HqlBaseParser.java:2806)
   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:2057)
   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)
   ... 7 more



Can someone help me make my query run without renaming my property?

Thanks in advance for your help!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 24, 2005 3:28 am 
Beginner
Beginner

Joined: Tue Nov 22, 2005 4:53 pm
Posts: 41
Location: Netherlands
'right' is also a normal SQL keyword, not only HQL ;)

And I think everybody knows that you should always avoid using keywords as columnames/properties.

So I don't see the problem of just renaming your property?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 24, 2005 3:54 am 
Newbie

Joined: Mon Nov 29, 2004 10:05 am
Posts: 5
momania wrote:
'right' is also a normal SQL keyword, not only HQL ;)

And I think everybody knows that you should always avoid using keywords as columnames/properties.

So I don't see the problem of just renaming your property?


I agree, but the problem is that calling that property right is most appropriate in the context of my application, and i would have hoped that the framework would be more flexible regarding this. I'm even somewhat surprised that the HQL interpreter still finds the aliased/prefixed syntax ambiguous...

I consider renaming the property the last resort.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 24, 2005 3:57 am 
Beginner
Beginner

Joined: Tue Nov 22, 2005 4:53 pm
Posts: 41
Location: Netherlands
sflamme wrote:
I'm even somewhat surprised that the HQL interpreter still finds the aliased/prefixed syntax ambiguous...

That surprises me aswell. Allthough it is a keyword, when used with an alias you may assume it to just work.

I even think that with normal SQL it will work when using an alias for the table. (maybe you can test that?)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 24, 2005 4:12 am 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
sflamme - I would take a look at using the Criteria API ;)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 24, 2005 4:17 am 
Beginner
Beginner

Joined: Tue Nov 22, 2005 4:53 pm
Posts: 41
Location: Netherlands
dennisbyrne wrote:
sflamme - I would take a look at using the Criteria API ;)

True, thats easier, but still for this case is only a work-around where as I think when using an alias in this query it should just work correctly. :)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 24, 2005 4:46 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
enter an enhacement request to jira. the translator does have a notion of keywords that can be used as identifiers, but it currently does not consider right/left.


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