Asked • 05/05/19

Account Balance verification with Java and Hibernate HQL?

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

1 Expert Answer

By:

Ben A. answered • 03/27/20

Tutor
4.7 (137)

Professional Java/Android Software Developer and Coding Guru

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.