My Solution for this:
__________________________
Code:
package my.app;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.criterion.CriteriaQuery;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.spi.TypedValue;
/**
* A criterion representing a "like" expression without diacritic signs.
*
* @author xsicdt based on LikeExpression from Hibernate
*/
public class LikeDiacriticlessExpression implements Criterion {
private static final long serialVersionUID = 1L;
private final String propertyName;
private final Object value;
private final Character escapeChar;
private final boolean ignoreCase;
private final String TRANSLATION = "'àâäçéèëêùûüôöïî', 'aaaceeeeuuuooii'";
public LikeDiacriticlessExpression(
String propertyName,
String value,
Character escapeChar,
boolean ignoreCase) {
this.propertyName = propertyName;
this.value = value;
this.escapeChar = escapeChar;
this.ignoreCase = ignoreCase;
}
public LikeDiacriticlessExpression(
String propertyName,
String value) {
this( propertyName, value, null, false );
}
public LikeDiacriticlessExpression(
String propertyName,
String value,
MatchMode matchMode) {
this( propertyName, matchMode.toMatchString( value ) );
}
public LikeDiacriticlessExpression(
String propertyName,
String value,
MatchMode matchMode,
Character escapeChar,
boolean ignoreCase) {
this( propertyName, matchMode.toMatchString( value ), escapeChar, ignoreCase );
}
public String toSqlString(
Criteria criteria,
CriteriaQuery criteriaQuery) throws HibernateException {
Dialect dialect = criteriaQuery.getFactory().getDialect();
String[] columns = criteriaQuery.findColumns(propertyName, criteria);
if ( columns.length != 1 ) {
throw new HibernateException( "Like may only be used with single-column properties" );
}
String escape = escapeChar == null ? "" : " escape \'" + escapeChar + "\'";
String column = columns[0];
StringBuffer sb = new StringBuffer();
sb.append("translate(");
sb.append(dialect.getLowercaseFunction());
sb.append('(');
sb.append(column);
sb.append("),");
sb.append(TRANSLATION);
sb.append(") like translate(");
sb.append(dialect.getLowercaseFunction());
sb.append("(?),");
sb.append(TRANSLATION);
sb.append(')');
sb.append(escape);
return sb.toString();
}
public TypedValue[] getTypedValues(
Criteria criteria,
CriteriaQuery criteriaQuery) throws HibernateException {
return new TypedValue[] {
criteriaQuery.getTypedValue( criteria, propertyName, ignoreCase ? value.toString().toLowerCase() : value.toString() )
};
}
}
_________________________________
Use this as follow:
Code:
criteria.add(new LikeDiacriticlessExpression(field, query, MatchMode.EXACT, null, true));
Please note that translate() could not exist on all databases.