-->
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.  [ 11 posts ] 
Author Message
 Post subject: Escaping HQL keywords in queries?
PostPosted: Fri Mar 06, 2009 6:25 pm 
Beginner
Beginner

Joined: Sun Jan 11, 2009 2:47 pm
Posts: 21
In HQL, what is the proper way to escape field names that might be HQL keywords? E.g. in " WHERE where='here' ", what goes around "where"?

I checked the documentation but this info appears to be missing. I've tried backticks, square braces, and double quotes to no avail.

Thanks!


Last edited by mnbv0987 on Tue Mar 10, 2009 4:28 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 10, 2009 4:27 am 
Beginner
Beginner

Joined: Sun Jan 11, 2009 2:47 pm
Posts: 21
Does anybody here have any idea? This seems like it should be really simple, but I have not discovered anything yet. I've tried single quotes as well.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 10, 2009 7:13 am 
Regular
Regular

Joined: Wed Oct 15, 2008 6:59 am
Posts: 103
Location: Chennai
just use single quote for this around string constant ,,
select u.name from User u where u.address like '%NEW%'

from User u where u.name='Madan'

_________________
If u feel it will help you, don't forget to rate me....


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 10, 2009 7:34 am 
Beginner
Beginner

Joined: Sun Jan 11, 2009 2:47 pm
Posts: 21
Madan_Prabhu wrote:
just use single quote for this around string constant ,,
select u.name from User u where u.address like '%NEW%'

from User u where u.name='Madan'


Thanks, but I want to escape field names, not field values. Like in the example I gave: The second where in WHERE where='here'.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 10, 2009 8:23 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
I think you can put the same escape char which is used in native SQL, like [] in mssql, "" in oracle etc...

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 10, 2009 3:22 pm 
Regular
Regular

Joined: Mon Jan 05, 2009 6:42 pm
Posts: 99
Location: IL
To extend the example as Madan did,if your search word is perter's then
your query would be
from User u where u.name='peter''s'


Top
 Profile  
 
 Post subject: Keywords
PostPosted: Tue Mar 10, 2009 8:17 pm 
Newbie

Joined: Wed Jul 11, 2007 12:28 pm
Posts: 5
I don't think it can be done, like Java, there are some reserved words you can't use as identifiers.

mnbv0987 wrote:
Madan_Prabhu wrote:
Thanks, but I want to escape field names, not field values. Like in the example I gave: The second where in WHERE where='here'.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 11, 2009 2:06 am 
Beginner
Beginner

Joined: Sun Jan 11, 2009 2:47 pm
Posts: 21
littypreethkr wrote:
I think you can put the same escape char which is used in native SQL, like [] in mssql, "" in oracle etc...


No, I tried this first, because it does work for column names in the Hibernate mapping file. I'm using mssql, putting square braces in the query makes Hibernate complain about an invalid left square brace in the HQL.

lielar wrote:
I don't think it can be done, like Java, there are some reserved words you can't use as identifiers.


There has to be a way... every DBMS has a way to escape field names that could be keywords, I'd have a hard time believing the Hibernate folks forgot to add that.

Also since most DBMS's have their own extensions to SQL, there's no way to actually know what the keywords are, so without a way to escape field names in HQL strings, you're never guaranteed that your query will work...

I mean in my particular case, the problematic field is named "key", which is a perfectly legitimate name for a field. Except "key" is a keyword in MS SQL, and now I have no way to construct those HQL queries for MS SQL...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 11, 2009 2:18 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
Yes I was wrong that you have to quote as per the DB. Kewl... Hibernate handles this automatically... You will have to quote them in the xml mapping file. See this:
http://www.hibernate.org/hib_docs/v3/re ... dentifiers

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 11, 2009 2:26 am 
Beginner
Beginner

Joined: Sun Jan 11, 2009 2:47 pm
Posts: 21
littypreethkr: Thank you so much, you have no idea how glad I am to get that solved. I had been using square brackets in the hibernate mapping, not backticks, and I guess it was confusing the HQL parser even though it worked in other situations. Switching to backticks in the mapping solved the problem.

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 17, 2009 9:28 am 
Newbie

Joined: Thu Feb 26, 2009 8:21 pm
Posts: 4
How about this(NHibernate with C#) when using query:
Code:
Expression.Like("where", "KEYWORD")


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