繁体   English   中英

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

[英]Account Balance verification with Java and Hibernate HQL

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

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

天平仅执行两个操作。 存款和取款。

问题是:

如何创建HQL查询,该查询将:

-将所有userdepositAmount
-求和所有user withdrawalAmount金额
-从第二求和中减去第一求和的结果
-将减法结果与currentBalanceFlag等于true Balance对象中的user amount进行比较

用伪代码:

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
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

这将返回所有流量的总和与当前余额之间的差。

附带说明,您不必检查数据的完整性。 除非您有成千上万的行,否则用SQL和计算当前余额应该足够快。

暂无
暂无

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

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