-->
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.  [ 4 posts ] 
Author Message
 Post subject: Searсhing escape symbols using criteria based search
PostPosted: Mon Apr 17, 2006 4:55 am 
Newbie

Joined: Mon Apr 17, 2006 4:35 am
Posts: 1
Location: Ukraine
Good day!

I'm using Oracle 10i database.
There are some rows in the table (Account) where Account_Name column data contains escape symbols. For example, 'SUB_'

Using HQL it is not a problem to search data with symbol '_', explicit ESCAPE definition will be used.
Code:
select account_name from account
where account_name like '%SUB\_' ESCAPE '\'


But when I use criteria based search, there is a problem.
Code:
criteria.add( Expression.like("account_name", "%SUB\_") );

The result query will look like the following:

Code:
select account_name from account
where account_name like '%SUB\_'


There is no explicit ESCAPE symbol definition, consequently the query returns incorrect data (in my case - empty result set).

So, the questions are:
1) Can I search data with escape symbols using Hibernate Criteria?
2) How can I do this?

Thanks in advance!


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 03, 2007 8:05 pm 
Newbie

Joined: Wed Feb 14, 2007 6:24 pm
Posts: 5
The org.hibernate.criterion.LikeExpression class can do this. There's a constructor that looks like this which gives you the ability to specify an escape character:

protected LikeExpression(
String propertyName,
String value,
Character escapeChar,
boolean ignoreCase)

Unfortunately it's protected. I just copied the source to a new class in one of my own packages and made the constructors public.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 04, 2007 8:42 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
I believe that if you use a parameterized query that setString will properly escape the characters. Let us know how that works out, I may need it in the future.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 04, 2007 2:46 pm 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
I used something like below with similar requirements


Code:
Criteria criteria = session.createCriteria( MyPersistent.class );
Criterion nameSQL = Restrictions.sqlRestriction( " this_.NAME_COL LIKE '%\\%%' ESCAPE '\\' " );
Criterion codeSQL = Restrictions.sqlRestriction( " this_.CODE_COL LIKE '%\\%%' ESCAPE '\\' " );
criteria.add( Restrictions.or( codeSQL, nameSQL ) );


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