使用 JPA CriteriaBuilder 合并所有并求和 [英] Union All and Sum with JPA CriteriaBuilder

查看:29
本文介绍了使用 JPA CriteriaBuilder 合并所有并求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将原生 SQL 查询转换为使用 JPA 2.0 中的 Criteria API.我在 Google 上找到了很多 Criteria API 示例,但我很难将所有部分放在一起.我希望有经验的人能够帮助我.本机查询如下所示:

I am trying to convert a native SQL query to use the Criteria API in JPA 2.0. I have found a lot of Criteria API examples on Google, but I am having a really hard time putting all of the pieces together. I'm hoping that a more experienced person will be able to help me out. The native query looks like this:

select 
    sum(amount) from firstTable, secondTable 
        where firstTable.id = secondTable.id 
            and amount <> 0 
            and firstTable.id = ?
union all
select 
    sum(amount) from firstTable, thirdTable 
        where firstTable.id = thirdTable.id 
            and amount <> 0 
            and firstTable.id = ?

原始查询结果集返回 BigDecimal 对象列表.

The original query result set was returning a List of BigDecimal objects.

谢谢!

推荐答案

JPA 不支持 UNION,要么使用原生 SQL 查询,要么执行两个查询.

JPA does not support UNION, either use a native SQL query, or execute two queries.

这篇关于使用 JPA CriteriaBuilder 合并所有并求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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