-->
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.  [ 3 posts ] 
Author Message
 Post subject: Criteria and wild character escape question
PostPosted: Thu May 18, 2006 11:28 pm 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
I have a question as how to escape wild characters while using criteria.

http://opensource.atlassian.com/project ... se/HHH-284 discusses similar question with HQL's.

For example, if want to search all names which have a "ABC" string my criteria query would be

Code:
Criteria criteria = session.createCriteria( MyPojo.class );
criteria.add( Restrictions.like( "name", "%ABC%" ) );
List results = criteria.list();


and the above code is working perfectly.

But if I want to search with names which have a "%" character in it

Code:
Criteria criteria = session.createCriteria( MyPojo.class );
criteria.add( Restrictions.like( "name", "%%%" ) );
List results = criteria.list();


the above code will not work as expected. I was escaping the % character in %%% with %\\%% but still I am getting every name. Is there any other way to escape these wild character search?

With following names availabale,
names ( user%one, ABC%one, ABC, user, XYZ%, %XYZ )
the results should be
results ( user%one, ABC%one, XYZ%, %XYZ )

Has anyone had to work with such a scenario, and if could you guide me in getting some information atleast that would be helpful.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 22, 2006 11:37 am 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
Does anyone know how to make the following SQL

select *
from TableA where id like '%\%%' escape '\'

to work using Criteria API.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 22, 2006 1:01 pm 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
I made the following code to work

Code:
//String expression = "(%\\%%)  ESCAPE ('\\')"; // this didnt worked
String expression = "'%\\%%' ";
Criteria crit = session.createCriteria( MyTableA.class );
crit.add( Restrictions.sqlRestriction( " a_id like "+ expression +" ESCAPE '\\'" ) );


to generate following sql

select *
from TableA where id like '%\%%' escape '\'

where a_id is the database column in tableA and mapped with hibernate id as

Code:
<class name="MyTableA" table="TABLEA">
   <id name="id" column="a_id"/>
   ....
</class>


however in this forum post (http://forums.oracle.com/forums/message.jspa?messageID=1274832) they discuss also this issue and my try with such a solution has not worked.

can anyone, if they have a chance to work with such problem, guide me?

Is there anything wrong in the approach I took by using Restrictions.sqlRestriction( ... ); as I am still not satisifed with the solution I have. There should be a cleaner approach.


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