aspose file tools*
The moose likes Object Relational Mapping and the fly likes Escape HQL Like special characters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Escape HQL Like special characters" Watch "Escape HQL Like special characters" New topic
Author

Escape HQL Like special characters

Sharon whipple
Ranch Hand

Joined: Jul 31, 2003
Posts: 294
Hi all
When executing HQL with like clause:
from Cat where name like “%” + myStr + “%”

For example in MSSQL escaping the % character will be:
Select * from cat where name like “%[%]%”
I think oracle will be:
Select * from cat where name like “%\%%”


How do I escape special characters?
Will hibernate escape characters for specific DB type?


Thank you
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Use parameter binding.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Sharon whipple
Ranch Hand

Joined: Jul 31, 2003
Posts: 294
When running:


I am getting all rows from the table, instead of getting specific row containing % sign
Do i have to escape % character before calling setParameter?


Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Sorry - read your question too quickly. I think you can use the escape keyword, though I can't find this documented, though I did find this in the Hibernate forums.
Sharon whipple
Ranch Hand

Joined: Jul 31, 2003
Posts: 294
Thank you for the link, Gavin stated that escape exist in Hibernate 3 (foo like <pattern> escape <escape-char>)
I am using older version of Hibernate...

Any other ideas?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

I can't think how you would do this in Hibernate 2 without specifying an escape character. You may just have to introduce some database specific here unfortunately.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Escape HQL Like special characters
 
Similar Threads
How to Retrive '_ ' from database
How to do a SELECT WHERE a LIKE b when b has a '%'?
java sql query
HQL: How to count green eye kittens?
Problem with escape chars...