Hibernate Criteria条件A-B> C

时间:2015-02-19 09:04:09

标签: java sql hibernate criteria

如何定义hibernate准则来生成SQL:

SELECT * FROM table WHERE columnA - columnB > anyInteger

? 我知道有:

Restrictions.sqlRestriction("column_a - column_b > "+int);

但它依赖于列名,而不是类属性。有什么办法解决它?

2 个答案:

答案 0 :(得分:0)

如果您希望能够对列进行查询,我认为您无法避免必须在列名称方面表达限制,因为计算必须在数据库端而不是Java端进行。但你可以做的是将一个额外的属性映射为formula,以便将SQL封装在Hibernate映射定义中,而不是每次查询时都重复它:

@Formula("column_a - column_b")
private int difference;

然后您将其查询为

Restrictions.gt("difference", value)

答案 1 :(得分:0)

好的,我找到了解决方案,而不是解决方法。我不得不创建Criterion类的新实现。这是代码:

public class DifferenceCriterion implements Criterion{

    private static final long serialVersionUID = -8082375591482621375L;

    private String firstProperty;
    private String secondProperty;
    private int value;

    public DifferenceCriterion(String propertyA, String propertyB, int value) {
        this.firstProperty = propertyA;
        this.secondProperty = propertyB;
        this.value = value;
    }

    @Override
    public TypedValue[] getTypedValues(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
        TypedValue tvFirst = criteriaQuery.getTypedValue(criteria, firstProperty, value);
        TypedValue tvSecond = criteriaQuery.getTypedValue(criteria, secondProperty, value);
        if(!tvFirst.equals(tvSecond)){
            throw new HibernateException("Properties typedValues are not the same!");
        }
        return new TypedValue[] {tvFirst};
    }

    @Override
    public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
        final String[] columnsA = criteriaQuery.getColumnsUsingProjection(criteria, this.firstProperty);
        final String[] columnsB = criteriaQuery.getColumnsUsingProjection(criteria, this.secondProperty);
        return columnsA[0] + " - "+columnsB[0] + " >  ?";
    }
}

并输入我的条件查询方法:

criteria.add(new DifferenceCriterion("columnA", "columnB", someInt));