Its pretty easy with HQL:
Code:
session.createQuery("from Entity where str(intPropertyName) like :val")
.setParameter("val", "%45%")
.list();
Can't see a way to apply an SQL function with the criteria API directly. Looks like you have to write your own custom Criterion:
Code:
session.createCriteria(Entity.class)
.add(new IntegerLikeExpression("intPropertyName", 45))
.list();
Here's a specific one covering your case:
Code:
public class IntegerLikeExpression implements Criterion {
private String propertyName;
private String likeValue;
public IntegerLikeExpression(String propertyName, int likeValue) {
this.propertyName = propertyName;
this.likeValue = "%"+String.valueOf(likeValue)+"%";
}
public TypedValue[] getTypedValues(Criteria criteria,
CriteriaQuery criteriaQuery) throws HibernateException {
return new TypedValue[] { new TypedValue(Hibernate.STRING, likeValue, EntityMode.POJO) };
}
public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery)
throws HibernateException {
SQLFunction toStringFunc = criteriaQuery.getFactory().getSqlFunctionRegistry().findSQLFunction("str");
List<String> funcArgs = new ArrayList<String>();
funcArgs.add(propertyName);
return toStringFunc.render(funcArgs, criteriaQuery.getFactory()) +" like ?";
}
}