Math operators in Criteria queries

一曲冷凌霜 提交于 2019-12-07 17:31:23

问题


Given the mapped hibernate class:

@Entity
public class MyTestClass {
  /* id and stuff */

  private Integer aValue;
  private Integer bValue;
}

you can do the following with HQL:

Query query 
  = getCurrentSession().createQuery("select aValue * bValue from MyTestClass");
List<Double> resultList = query.list;

and get the calculated result out.

Is it possible to do something similar to this with the Criteria API? I still haven't found a way to use math operations with the Criteria API. We have aggregate functions like sum, avg and so on, but not the basic math operators?


回答1:


You can make a new property in your class that is this computed value. Just specify the formula attribute for that property. Then you can include this property in your Criteria.

<property name="product" formula="aValue*bValue" />

formula (optional): an SQL expression that defines the value for a computed property. Computed properties do not have a column mapping of their own.




回答2:


you can always add it as sql I think there was some sqlProjection/sqlRestriction method



来源:https://stackoverflow.com/questions/747109/math-operators-in-criteria-queries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!