在 ormlite 中编写连接查询的更好方法

发布于 2024-12-02 23:42:54 字数 1127 浏览 1 评论 0原文

我有两个表 CustomerBalance 和 Customer,它们与 CustomerRefId 字段绑定。 我希望该表的字段余额的 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;
    }
} 

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

I have two tables CustomerBalance and Customer which are bound with CustomerRefId field.
I want the CustomerBalance records that are lets say greater than 100 for a field balance of this tables. I also want to include into my results the name of the particular customer that fulfills that criteria. I created the following method that works!

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;
    }
} 

This methods is working, is this the best way to write such a query? or is there a more appropriate approach?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

携君以终年 2024-12-09 23:42:54

对查询的一项改进是使用 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 语句。

One improvement to your query is to use ORMLite's SelectArg to pass in the customer-id instead of a new query each time. Something like:

...
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);
}

Here are the docs for SelectArg:

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

FYI, there also is an UpdateBuilder, but I don't see an easy way to turn your code above into a single UPDATE statement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文