-->
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: Using properties in WHERE clause of a SQLQuery
PostPosted: Wed Feb 18, 2009 9:34 am 
Newbie

Joined: Wed Feb 18, 2009 8:17 am
Posts: 3
Hi,

I have trouble using properties (instead of column names) in the WHERE clause of a SQLQuery.
For example:
Code:
SQLQuery q = session.createSQLQuery("SELECT {insect.*} FROM insect {insect} WHERE {insect}.numberOfLegs=6").addEntity("insect", Insect.class);


This results in "Unknown column 'insect.numberOfLegs' in 'where clause'".
The column name in the database is number_of_legs.

I tried to inject the alias name using "WHERE {insect.numberOfLegs}=6" but then the database also complains with something like: "Unknown column 'number6_1093_0_' in 'where clause'". I guess this is because MySQL does not allow aliases in the WHERE clause (http://dev.mysql.com/doc/refman/5.0/en/problems-with-alias.html).

Is it at all possible to use properties instead of column names in the WHERE clause?

Hibernate version: 3.3.1 GA


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 18, 2009 9:41 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Why don't you just use a HQL-Query like:
"select i from Insect i where i.numberOfLegs=6"?

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 18, 2009 9:43 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
double post

_________________
-----------------
Need advanced help? http://www.viada.eu


Last edited by mmerder on Wed Feb 18, 2009 9:46 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 18, 2009 9:45 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
double post


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 18, 2009 10:11 am 
Newbie

Joined: Wed Feb 18, 2009 8:17 am
Posts: 3
mmerder wrote:
Why don't you just use a HQL-Query like:
"select i from Insect i where i.numberOfLegs=6"?


The reason I went for an SQLQuery instead of HQL in the first place was that I could not use RAND(<seed>) from HQL. (The query in my post was just an example)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 18, 2009 10:26 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
As far as I know using properties in native SQL is not possible.

If you want HQL-Parser to recognize SQL-functions, you have to subclass your dialect and call registerFunction().

That may look like this:
Code:
public class YourDialect extends MySQLDialect {
  public YourDialect {
    registerFunction("yourFunction", new StandardSQLFunction("yourFunction", Hibernate.INTEGER) );
  }
}

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 19, 2009 4:51 pm 
Newbie

Joined: Wed Feb 18, 2009 8:17 am
Posts: 3
mmerder wrote:
As far as I know using properties in native SQL is not possible.

If you want HQL-Parser to recognize SQL-functions, you have to subclass your dialect and call registerFunction().

That may look like this:
Code:
public class YourDialect extends MySQLDialect {
  public YourDialect {
    registerFunction("yourFunction", new StandardSQLFunction("yourFunction", Hibernate.INTEGER) );
  }
}


Thanks! Extending MySQLDialect and using HQL worked for me in this case.

But I still think that property names in the where clause of SQLQueries would be nice to have in other cases. I found this phrase in the documentation:
"Notice that we may even use the property aliases in the where clause if we like. "
But it does not work (at least not with MySQL). Maybe it's a bug?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 19, 2009 6:35 pm 
Senior
Senior

Joined: Thu Jan 08, 2009 3:48 pm
Posts: 168
http://www.hibernate.org/hib_docs/v3/re ... references

I think the correct syntax for your example should be
Code:
FROM insect i where {i.numberOfLegs}=6

or
Code:
FROM insect i where {insect.numberOfLegs}=6

if alias references the "addEntity" statement.

rating is welcome


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.