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

时间:2012-08-28 15:47:08

标签: jpa jpa-2.0

我在两个不同的实体中用JPA计算了两笔钱:

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

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

我需要的是这笔钱的总和。 JPAQL有可能吗?或者我需要运行两个单独的查询并将其添加到应用程序中?

2 个答案:

答案 0 :(得分:1)

你不能严格遵守JPQL。

在SQL中,它将类似于

SELECT SUM(a)FROM(从DebtorAccount UNION SELECT中选择da.debtorBalance a mda.debtorBalance a FROM MasterDebtorAccount)

JPA不支持UNION,尽管有几个实现。

不,如果没有联盟,你不能这样做,因为关系代数意味着你从DebtorAccount * MasterDebtorBalance开始,之后你通过条件/ aplying函数来改进它。

答案 1 :(得分:0)

你尝试过吗?

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

Regadrs,

相关问题