-->
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.  [ 8 posts ] 
Author Message
 Post subject: Selection expressions as porjections
PostPosted: Wed Oct 25, 2006 6:58 pm 
Newbie

Joined: Wed Oct 25, 2006 6:39 pm
Posts: 2
Location: Vienna
hey!

i would like to do the following query:

SELECT i.name='john' FROM test.Individual i.

while the equivalent in SQ works perfectely fine, hibernate does not seem to be able to parse the query:

SEVERE: *** ERROR: <AST>: unexpected AST node: =
Exception in thread "main" org.hibernate.hql.ast.QuerySyntaxError: unexpected AST node: = [SELECT i.name='john' FROM test.Individual i]
at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:63)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:196)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:130)
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 org.makumba.view.HibernateQueryRunner.executeDirect(HibernateQueryRunner.java:118)
at org.makumba.view.HibernateQueryRunner.main(HibernateQueryRunner.java:194)
Caused by: <AST>: unexpected AST node: =
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectClause(HqlSqlBaseWalker.java:1036)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:380)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:201)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:151)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:189)
... 8 more


I checked all the documents i could find about hibernate query language, but selecting expressions in projections was not discussed anywhere.

so, my question is, do i make the query wrong, or is this not (yet) supported?

i tried this in hibernate 3.0.5 and in 3.2.0, the same problem occurs in both.

thx!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 26, 2006 1:56 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
doing an assignment in the SELECT clause does not make sense in HQL.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 29, 2006 5:24 pm 
Newbie

Joined: Wed Oct 25, 2006 6:39 pm
Posts: 2
Location: Vienna
Maybe i did not make myself clear enough, but i do not want to make an assignement in the projection, but i want to select an expression, in the example a test for equality.
in the example given, i would expect true/false or 0/1 as the value from the projection "i.name='john'" - i do not want to assign a value to the field name in the object Individual.

maybe '=' is not the right operator to use for equality testing, but i did not find anything else in the HQL documentation, '=' is used in WHERE clauses, and both '==' and 'eq' are not know to HQL either.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 29, 2006 5:36 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
hql doesn't have support for expressions (besides functions) in the select part.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 31, 2006 6:43 am 
Newbie

Joined: Mon Aug 22, 2005 9:55 am
Posts: 4
max wrote:
hql doesn't have support for expressions (besides functions) in the select part.


hmm, then why does SELECT a+b FROM... work?

also, aren't expressions needed for report queries? are people just supposed to report functions?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 31, 2006 7:13 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
actually a good question ;)

expressions are actually allowed in some cases, but it doesn't prevent you having report queries since you can model all these as function calls.

Report a bug on it and lets see if there is a reason why = doesn't work.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 05, 2007 4:31 pm 
Newbie

Joined: Mon Mar 05, 2007 3:50 pm
Posts: 2
Location: Ljubljana, Slovenia
Playing a bit with this problem i came to the conclusion that logical expressions (= > < ...) are probably not directly supported in HQL because they are not supported by Oracle and MS SQL dialects, while experience with MySQL can give the impression that this should be possible.

MySQL:
select 1+2: 3
select 1>2: 0

Oracle:
select 1+2 from dual: 3
select 1>2 from dual: ORA-00923 FROM keyword not found where expected

MS SQL:
select 1+2: 3
select 1>2: Incorrect syntax near '>'.

in HQL it would be useful to have support for such expressions.

One easy way to evaluate boolean expressions in the DBMS would be to write the SQL with
CASE WHEN logical_expression THEN 1 ELSE 0
like:
select CASE WHEN 1>2 THEN 1 ELSE 0
which is supported by all of the above SQL dialects

...but according to docs
http://www.hibernate.org/hib_docs/v3/re ... xpressions
and grammar
http://hibernate.cvs.sourceforge.net/hi ... mmar/hql.g
case...when...then...else...end construct is only possible in the
where condition of HQL.

Easy fix would be to fix grammar to allow case...when in select part of HQL because it can be easy transcribed to SQL (valid in all SQL92 compliant SQL dialects).

More advanced fix would be to detect logical expressions in the select part of HQL and translate them into "case when logical_expression then 1 else 0 end".


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 09, 2007 5:38 am 
Newbie

Joined: Mon Mar 05, 2007 3:50 pm
Posts: 2
Location: Ljubljana, Slovenia
Also postgresql also supports such syntax:
http://www.postgresql.org/docs/8.2/inte ... ional.html

Related issues:
Hibernate: http://opensource.atlassian.com/project ... se/HHH-467
NHibernate: http://jira.nhibernate.org/browse/NH-322


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