Criteria查询中的数学运算符 [英] Math operators in Criteria queries

查看:350
本文介绍了Criteria查询中的数学运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定映射的hibernate类:

  @Entity 
public class MyTestClass {
/ * id和东西* /

私人整数aValue;
私人整数b值;
}

您可以使用HQL执行以下操作:

 查询查询
= getCurrentSession()。createQuery(从MyTestClass中选择aValue * bValue);
列表< Double> resultList = query.list;

并计算结果。

是否有可能使用Criteria API做类似于此的事情?我仍然没有找到使用Criteria API使用数学运算的方法。我们有总和函数,如sum,avg等,但不是基本的数学运算符?解析方案

您可以创建一个新的属性在你的班级,这是计算的价值。只需指定该属性的公式属性即可。然后你可以在你的标准中包含这个属性。

 < property name =productformula =aValue * bValue/ >公式(可选):一个SQL表达式,用于定义计算属性的值。

计算属性没有自己的列映射。

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?

解决方案

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.

这篇关于Criteria查询中的数学运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆