0

我有两个与 CustomerRefId 字段绑定的表 CustomerBalance 和 Customer。对于此表的字段余额,我希望 CustomerBalance 记录大于 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;
    }
} 

这种方法有效,这是编写此类查询的最佳方法吗?还是有更合适的方法?

4

1 回答 1

2

对您的查询的一项改进是使用ORMLite SelectArg来传递客户 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语句的简单方法。

于 2011-09-05T13:39:50.930 回答