-->
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.  [ 7 posts ] 
Author Message
 Post subject: HQL : how to escape % and _ in like expression
PostPosted: Fri Jun 24, 2005 9:51 am 
Newbie

Joined: Fri Jun 24, 2005 5:28 am
Posts: 3
Hibernate version: 2

Name and version of the database you are using: PostgreSQL, but would like to be DB independant

Problem:
I need to be able to escape both % and _ (probably the escaped char too) in with a like clause in a find.

Example:

from DomesticCat cat where cat.name like 'FRI%'

What do I do if I only want the cat named FRI%?

I tried to backslash the \ or the _ but it doesn't seem to work. Tryed also to do it the same way it is done in the like in Postgresql ('\\\%') but it doesn't work too.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 24, 2005 10:26 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
In HB3 you can type:

Code:
foo like <pattern> escape <escape-char>



Note sure if this works in HB2.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 24, 2005 11:24 am 
Newbie

Joined: Fri Jun 24, 2005 5:28 am
Posts: 3
Thanks for the info, I may try it too.

But I just found a way:

- updated to last Hibernate (2.1.8)

- (Tested with PostgreSQL, hope it's not base dependant)

\ remplaced by \\\\
Code:
replaceAll("\\\\", "\\\\\\\\\\\\\\\\")


% are escaped with \\\%
Code:
replaceAll("%","\\\\\\\\%")


_ are remplaced by \\\_
Code:
replaceAll("_", "\\\\\\\\_")


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 27, 2005 3:54 am 
Newbie

Joined: Fri Jun 24, 2005 5:28 am
Posts: 3
This is base dependant.

I also needed to change like for ilike as I needed a caseless comparison. (and ilike is how you do it in postgresql).


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 19, 2005 6:00 am 
Regular
Regular

Joined: Tue Jan 06, 2004 3:32 pm
Posts: 80
Location: Munich, Germany
gavin wrote:
In HB3 you can type:

Code:
foo like <pattern> escape <escape-char>




Can the escape char also be defined in a Criteria query?

What about the Restrictions.like(String searchString, MatchMode) versions? In theory they would not need any wildcard char in the searchString, because the MatchMode says it all (either at the end, at the beginning, both, or none at all). What happens if I call Restrictions.like("FRI%", MatchMode.END)?

Regards,

Andreas


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 29, 2005 3:20 pm 
Newbie

Joined: Mon May 03, 2004 11:53 pm
Posts: 7
The following worked in Hibernate 3:
criteria.add(Restrictions.ilike("nameTx", "B\\_1", MatchMode.ANYWHERE));

The following did not appear to work:
criteria.add(Restrictions.ilike("nameTx", "B_1 escape _", MatchMode.ANYWHERE));

srceString.replaceAll("_", "\\_")


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 27, 2005 1:47 pm 
Beginner
Beginner

Joined: Sat Nov 13, 2004 7:21 pm
Posts: 24
guest008 wrote:
Thanks for the info, I may try it too.

But I just found a way:

- updated to last Hibernate (2.1.8)

- (Tested with PostgreSQL, hope it's not base dependant)

\ remplaced by \\\\
Code:
replaceAll("\\\", "\\\\\\\\\\\\\\\")


% are escaped with \\\%
Code:
replaceAll("%","\\\\\\\\%")


_ are remplaced by \\\_
Code:
replaceAll("_", "\\\\\\\\_")

This doesn't work with HSQLDB.


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