2

現在のアカウント残高オブジェクトには次のプロパティがあります。

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

天びんに対して実行されるアクションは2つだけです。入出金。

質問は:

次のようなHQLクエリを作成するにはどうすればよいですか?

-sum all for depositAmount-sumallfor -2番目の合計から最初の合計の 結果を減算します-減算の結果を次の値を持つforinオブジェクトと比較しますuser
withdrawalAmountuser

amountuserBalancecurrentBalanceFlagtrue

擬似コード:

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
4

1 に答える 1

1
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の合計を使用して現在の残高を計算するのは十分に高速です。

于 2011-01-30T10:42:18.860 に答える