質問

CustomerRefidフィールドにバインドされている2つのテーブルカスタマーバランスと顧客がいます。このテーブルのフィールドバランスについては、100を超えると言えば、カスタマーバランスレコードが必要です。また、その基準を満たす特定の顧客の名前を自分の結果に含めたいと思います。動作する次の方法を作成しました!

public List<CustomerBalance> getCustomerBalanceFilter(String filterVal) {
    try {
        PreparedQuery<CustomerBalance> preparedQuery = mDbHelper.getCustomerBalanceDao().queryBuilder()
            .where().gt(CustomerBalance.DB_COL_CUSTOMER_BALANCE, filterVal)
            .prepare();
        List<CustomerBalance> result = mDbHelper.getCustomerBalanceDao().query(preparedQuery); 

        for(CustomerBalance alert : result) {
            PreparedQuery<Customer> getCustQuery = mDbHelper.getCustomerDao().queryBuilder()
                .where().eq(Customer.DB_COL_CUSTOMER_REF_ID, alert.getCustomerID())
                .prepare();
            List<Customer> customer = mDbHelper.getCustomerDao().query(getCustQuery);

            alert.setCustomer(customer.size() == 1 ? customer.get(0) : null);
        }

        return result;

    } catch(Exception ex) {
        return null;
    }
} 

この方法は機能していますが、これはそのようなクエリを書くための最良の方法ですか?または、より適切なアプローチはありますか?

役に立ちましたか?

解決

クエリの1つの改善は、使用することです ormlite's SelectArg 毎回新しいクエリの代わりに、customer-idに渡すこと。何かのようなもの:

...
List<CustomerBalance> result = mDbHelper.getCustomerBalanceDao()
    .query(preparedQuery); 

SelectArg custIdArg = new SelectArg();
PreparedQuery<Customer> getCustQuery = mDbHelper.getCustomerDao().queryBuilder()
    .where().eq(Customer.DB_COL_CUSTOMER_REF_ID, custIdArg)
    .prepare();
for (CustomerBalance alert : result) {
    custIdArg.setValue(alert.getCustomerID());
    List<Customer> customer = mDbHelper.getCustomerDao().query(getCustQuery);
    alert.setCustomer(customer.size() == 1 ? customer.get(0) : null);
}

これがドキュメントです SelectArg:

http://ormlite.com/docs/select-arg

参考までに、あります UpdateBuilder, 、しかし、上記のコードを単一に変える簡単な方法はありません UPDATE 声明。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top