-->
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: Using Escape Character with "LIKE" Criteria Query
PostPosted: Mon Jan 23, 2006 6:09 pm 
Newbie

Joined: Thu Jan 19, 2006 5:57 pm
Posts: 2
Hibernate version: 3.1

Code between sessionFactory.openSession() and session.close():

Code:
List topicList =
    getSession().createCriteria(Topic.class)
        .add(Expression.like("topicShortName", topicShortName,
                                      MatchMode.START))
        .list();


Name and version of the database you are using: DB2

The generated SQL (show_sql=true):

Code:
select this_.AUTOWR_GRP_ID as AUTOWR1_0_0_, this_.AUTOWR_TPC_SHRT_NA as AUTOWR2_0_0_, this_.AUTOWR_TPC_DESC as AUTOWR3_0_0_, this_.RCVR_UID_CD as RCVR4_0_0_, this_.MSG_PRI_CD as MSG5_0_0_ from AUTOWR_TPC_12753 this_ where this_.AUTOWR_TPC_SHRT_NA like ?


I know that this question has been asked in the past, but I did a search and found only unanswered posts.

I would like to use a Criteria Query to perform a LIKE wildcard search, but our business requirements allow for data containing underscores. Since underscores and percent signs are the SQL wildcards used with LIKE, a search for a field starting with "test_"% might return "test_page" and "tests". The users want to only see the row for "test_page" for this example.

I have gotten this to work using HQL and SQL by adding an "ESCAPE '\'" clause at the end of the provided query string. Is this possible using criteria queries? Is there a method that I have not found to provide an escape character that Hibernate would escape in the match criteria for like and or ilike?

Thanks in advance,
Ryan


Top
 Profile  
 
 Post subject: Custom criterion
PostPosted: Tue Jan 24, 2006 2:42 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
YOu may try creating own criterion like this:
public class EscLikeExpression implements Criterion {

private final String propertyName;
private final Object value;

protected EscLikeExpression(String propertyName, Object value) {
this.propertyName = propertyName;
this.value = value;
}

protected EscLikeExpression(String propertyName, String value, MatchMode matchMode) {
this( propertyName, matchMode.toMatchString(value) );
}

public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery)
throws HibernateException {
Dialect dialect = criteriaQuery.getFactory().getDialect();
String[] columns = criteriaQuery.getColumnsUsingProjection(criteria, propertyName);
if (columns.length!=1) throw new HibernateException("like may only be used with single-column properties");
return columns[0] + " like ? escape '#' ";

//TODO: get SQL rendering out of this package!
}

public TypedValue[] getTypedValues(Criteria criteria, CriteriaQuery criteriaQuery)
throws HibernateException {
return new TypedValue[] { criteriaQuery.getTypedValue( criteria, propertyName, value.toString().toLowerCase() ) };
}

public String toString() {
return propertyName + " ilike " + value;
}



And then use it:
session.createCriteria( My.class ).add( new EscLikeExpression( "name","test#_%") ).list();

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 24, 2006 5:36 pm 
Newbie

Joined: Thu Jan 19, 2006 5:57 pm
Posts: 2
Thank you for the response. I wondered if implementing a Criterion would do what I wanted, but being new to Hibernate, I wasn't sure.

FYI... I did have to make the constructors public in the above example code. Also, I removed the ".toLowerCase()" in getTypedValues() and modified the toString() implementation since I am using like instead of ilike.


Thanks again,
Ryan


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.