wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes hibernate mysql Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "hibernate mysql "Like"" Watch "hibernate mysql "Like"" New topic
Author

hibernate mysql "Like"

s mahen perera
Ranch Hand

Joined: Mar 08, 2005
Posts: 101
hi all,

I am having problems with issuing a HQL search query which has a "like" clause in it...the database that i am using is MySQL 5.0

for example my HQL is something like



I have highlighted the areas due to which the error is coming up.. i get some errors saying that the '%' + ? + '%' cannot be recognised , and that it is not correct MySQL syntax,,, when i investigated, i found out that mysql does not allow concatenation of strings to be done with '+' ,,

then i did something like LIKE '%?%' , but that also didnt work ,,

would appreciate if some one call tell a possible workaround for this,, basically i want to get thru this SQL query which uses "like"

Thanks
[ January 18, 2007: Message edited by: Mark Spritzler ]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

Why are you using single quotes?

It is not recognizing single quotes.

you need double quotes and you need to escape your double quotes with the "/"

so

f.folderName LIKE '%' + ? + '%'

would be

f.folderName LIKE /"%" + :someBindVariableName + "%/"

Also your code will be cleaner and easier to read and maintain if you use bind parameters instead of "?" positional parameters.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
s mahen perera
Ranch Hand

Joined: Mar 08, 2005
Posts: 101
Thanks,,

i tried it out,,
but, i am getting this error now..

11:09:56,361 ERROR PARSER:35 - *** ERROR: line 1:196: unexpected token: /

i didnt use bind names though,, used the same old ? s as positional parameters

Thanks
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Looks like you've just made a mistake with your syntax. Check it carefully. If you can't see the problem, post your query here and we'll have a look.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
s mahen perera
Ranch Hand

Joined: Mar 08, 2005
Posts: 101
hi,
thanks,,

As Mark suggested,

i replaced the like sections with something like this

f.folderName LIKE /"%" + ? + "%/"

and then i am getting the above error i mentioned..
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

since you have the query string posted we don't see the way it is constructed with your " and method calls, and while my method will work, it is still tough to determine when " in the variable assignment versus " in the query string sent to the database.



That is your query set to a string and escaping the double quotes where needed. I also took out you "?" and put in bind parameters instead. If you really had to use "?" which is a real pain to read and maintain, then take out the :bindName and replace it with ? do not put the ? with concatenating plus signs.

Mark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hibernate mysql "Like"