This is how I solved it:
criteria.add(new PropertyExpression(null, null, null) {
@Override
public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
String x = criteriaQuery.getColumnsUsingProjection(criteria, "x")[0];
String y = criteriaQuery.getColumnsUsingProjection(criteria, "tbl.y")[0];
StringBuilder result = new StringBuilder();
result.append(x);
result.append(">= (");
result.append(y);
result.append(" - 1)");
return result.toString();
}
});
shame there's no way, using Hibernate Criteria API, to do this (alias a joined table):
criteria.add(Restrictions.sqlRestriction("{alias}.x >= ({tbl}.y - 1)"));