使用Java和Hibernate HQL验证帐户余额

使用Java和Hibernate HQL验证帐户余额,java,hibernate,object,accounting,balance,Java,Hibernate,Object,Accounting,Balance,我的当前帐户余额对象中有以下属性: long id; // Database ID Date date; // date when this balance object was created boolean currentBalanceFlag; // indicates this is the most recent balance float amount; // the tota

我的当前帐户余额对象中有以下属性:

long id;                     // Database ID
Date date;                   // date when this balance object was created
boolean currentBalanceFlag;  // indicates this is the most recent balance
float amount;                // the total sum currently in the account balance
float depositAmount;         // amount deposited that resulted in this objects amount 
float withdrawalAmount;      // amount withdrawn that resulted in this objects amount
Balance lastBalance;         // last balance object for traversing
User user;                   // owner of the balance
String note;                 // detailed description of transaction that resulted in current blanace
天平上只执行两个动作。存款和取款

问题是: 如何创建将: -为
用户
合计所有
存款金额

-对
用户
的所有
取款金额进行求和
-从第二次求和中减去第一次求和的结果
-对于
余额
对象中
当前余额标志
等于

在伪代码中:

resultAmount = select ( sum(depositAmount) - sum(withdrawalAmount) ) from Balance where user=user
amount = select amount from Balance where user=user and currentBalanceFlag=true
我想通过HQL查询调用数据库得到的最后一个布尔结果是:

resultAmount == amount
这将返回所有流量之和与当前余额之间的差额

另一方面,您不必检查数据的完整性。除非有无数行,否则使用SQL求和计算当前余额应该足够快

select (sum(flow.depositAmount) - sum(flow.withdrawalAmount) - current.amount) 
from Balance flow, Balance current
where flow.user=:user
and current.user=:user 
and current.currentBalanceFlag=true