• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Escape HQL Like special characters

 
Sharon whipple
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use parameter binding.
 
Sharon whipple
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic