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

发布于 2024-10-14 23:40:29 字数 1371 浏览 2 评论 0原文

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

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 查询,该查询将:

-sum 所有 user
depositAmount -用户的所有withdrawalAmount总和
-用第二次求和减去第一次求和的结果
- 将减法结果与 Balance 对象中 useramount 进行比较,该对象的 currentBalanceFlag 等于 true

伪代码中的

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

I have following properties in my current Account Balance object:

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

There are only two actions performed on the balance. Deposits and withdrawals.

The question is:

How do I create HQL query that will:

-sum all the depositAmount for user
-sum all withdrawalAmount for user
-subtract result of the first summation from second summation
-compare result of subtraction with the amount for user in Balance object that has currentBalanceFlag equal to true

in pseudo code:

resultAmount = select ( sum(depositAmount) - sum(withdrawalAmount) ) from Balance where user=user
amount = select amount from Balance where user=user and currentBalanceFlag=true

And the final boolean result I'd like to get from single call to the database with HQL query:

resultAmount == amount

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巷子口的你 2024-10-21 23:40:29
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 总和计算当前余额应该足够快。

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

This will return the difference between the sum of all flows and the current balance.

On a side note, you shouldn't have to check the integrity of your data. Unless you have gazillions of lines, computing the current balance with an SQL sum should be fast enough.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文