使用 Java 和 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 查询,该查询将:
-sum 所有 user
的 depositAmount
-用户
的所有withdrawalAmount
总和
-用第二次求和减去第一次求和的结果
- 将减法结果与 Balance
对象中 user
的 amount
进行比较,该对象的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这将返回所有流量总和与当前余额之间的差额。
顺便说一句,您不必检查数据的完整性。除非您有无数行,否则使用 SQL 总和计算当前余额应该足够快。
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.