休眠标准条件 A - B > C
Hibernate Criteria condition A - B > C
如何定义休眠标准来生成SQL:
SELECT * FROM table WHERE columnA - columnB > anyInteger
?
我知道有:
Restrictions.sqlRestriction("column_a - column_b > "+int);
但它取决于列名,而不是 class 属性。有什么办法可以解决吗?
我认为如果你想查询列名,你就不能避免用列名来表达限制,因为计算必须发生在数据库端而不是 Java 边。但是你可以做的是将额外的 属性 映射为 formula 以将 SQL 封装在 Hibernate 映射定义中,而不是每次查询时都必须重复它:
@Formula("column_a - column_b")
private int difference;
然后您可以将其查询为
Restrictions.gt("difference", value)
好的,我找到了解决方案,而不是解决方法。我必须创建 Criterion class 的新实现。这是代码:
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));
如何定义休眠标准来生成SQL:
SELECT * FROM table WHERE columnA - columnB > anyInteger
? 我知道有:
Restrictions.sqlRestriction("column_a - column_b > "+int);
但它取决于列名,而不是 class 属性。有什么办法可以解决吗?
我认为如果你想查询列名,你就不能避免用列名来表达限制,因为计算必须发生在数据库端而不是 Java 边。但是你可以做的是将额外的 属性 映射为 formula 以将 SQL 封装在 Hibernate 映射定义中,而不是每次查询时都必须重复它:
@Formula("column_a - column_b")
private int difference;
然后您可以将其查询为
Restrictions.gt("difference", value)
好的,我找到了解决方案,而不是解决方法。我必须创建 Criterion class 的新实现。这是代码:
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));