Thread necromancy! (Since this seems like an open question for the previous poster, and it was an open question for me.)
Code:
private class TypelessLikeExpression implements Criterion {
private String propertyName;
private String value;
public TypelessLikeExpression(String propertyName, String value) {
this.propertyName = propertyName;
this.value = 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");
}
CastFunction cast = (CastFunction) dialect.getFunctions().get("cast");
String lhs = dialect.getLowercaseFunction() + '(' + cast.render(Arrays.asList(columns[0], "java.lang.String"), criteriaQuery.getFactory()) + ')';
return lhs + " like ?";
}
public TypedValue[] getTypedValues(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
return new TypedValue[] { new TypedValue(new org.hibernate.type.StringType(), MatchMode.START.toMatchString(value.toLowerCase()), EntityMode.POJO) };
}
}
This one obviously has MatchMode.START behavior hardcoded in to it, as well as case insensitivity. Those should be fairly straightforward to generalize though... do it the same way the original LikeExpression does it.