简体   繁体   English

JPA中两个不同实体的值总和

[英]Sum of values from two different entities in JPA

I have two sums to calculate in JPA, from two different entities: 我在JPA中有两个总和,分别来自两个不同的实体:

SELECT SUM(da.debtorBalance) FROM DebtorAccount da WHERE <conditions for DebtorAccounts>

and

SELECT SUM(mda.debtorBalance) FROM MasterDebtorAccount mda WHERE <conditions for MasterDebtorAccounts>

What I need is sum of this sums. 我需要的是这笔款项的总和。 Is it possible by JPAQL? JPAQL有可能吗? Or I need to run two separate queries and add this in the application? 还是我需要运行两个单独的查询并将其添加到应用程序中?

You can't in strict JPQL. 您不能使用严格的JPQL。

In SQL, it would be something like 在SQL中,它将类似于

SELECT SUM(a) FROM (SELECT da.debtorBalance a from DebtorAccount UNION SELECT mda.debtorBalance a FROM MasterDebtorAccount) SELECT SUM(a)FROM(从DebtorAccount UNION选择SELECT da.debtorBalance a SELECT Mda.debtorBalance一个FROM MasterDebtorAccount)

JPA does not support UNION, although several implementations do. JPA不支持UNION,尽管有几种实现。

And no, you can't do this without union because relational algebra means that you begin with DebtorAccount * MasterDebtorBalance and after that you refine it through conditions / aplying functions. 不,没有联合就不能做到这一点,因为关系代数意味着您要从DebtorAccount * MasterDebtorBalance开始,然后再通过条件/应用函数对其进行优化。

have you tried? 你有没有尝试过?

SELECT SUM(SUM(da.debtorBalance), SUM(mda.debtorBalance)) 
FROM DebtorAccount da, MasterDebtorAccount mda
WHERE (<conditions for DebtorAccounts>) OR (<conditions for MasterDebtorAccounts>)

Regadrs, Regadrs,

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

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