简体   繁体   English

基于条件构建器字符串-表达式quot为double-JPA

[英]Criteria Builder String based - Expression quot as double - JPA

New to CB, I'm trying to use it instead of native queries. CB的新手,我正在尝试使用它代替本机查询。 The reason is I'll need different grouping by situations. 原因是我需要根据情况进行不同的分组。 Here's what I'm need to reproduce: 这是我需要重现的内容:

(1 - sum( field_1 [integer])::real / sum ( field_2 [integer])::real  ) * 100

Here's the code which suppose to do that: 这是执行此操作的代码:

Expression<Double> filed1Sum = cb.sumAsDouble(report.<Float>get("field1"));
Expression<Double> filed2Sum = cb.sumAsDouble(report.<Float>get("field2"));
Expression<Double> expected = cb.toDouble(cb.<Float>quot(filed1Sum, filed2Sum));

The problem is: the expected value is coming as Long!!! 问题是:期望值即将达到!!! As the field1 is aways less than field2 I've got 0 (zero). 由于field1的距离小于field2,所以我得到了0(零)。 I'm wondering get 0.888 something like that. 我想知道0.888这样的东西。

I've solved by multiplying the dividend by 1.0D: 我通过将股息乘以1.0D来解决:

Expression<Number> expected = cb.quot(cb.prod(filed1Sum,cb.literal(1.0D)), filed2Sum);

That generates the query: 生成查询:

(sum(field1) * 1.0) / sum(field2) 

Which returns a floating point correctly. 正确返回浮点数。 No explicit conversions were required. 不需要显式转换。

Thank you for the efforts to help me 谢谢您对我的帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM