android – ORMLite – 查询外部字段

前端之家收集整理的这篇文章主要介绍了android – ORMLite – 查询外部字段前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
使用ORMLite for Android,我需要构建一个按订单ID或客户名称返回订单的查询.请考虑以下类声明:
@DatabaseTable(tableName = "order")
public class Order {
    @DatabaseField(generatedId = true)
    private Long id;

    @DatabaseField(foreign = true,canBeNull = false,foreignAutoRefresh = true,columnName = "customer_id")
    private Customer customer;

    // default constructor,getters and setters...
}

@DatabaseTable(tableName = "customer")
public class Customer {
    @DatabaseField(generatedId = true)
    private Long id;

    @DatabaseField
    private String name;

    // default constructor,getters and setters...
}

我正在寻找的原始sql将是这样的:

SELECT 
    o.* 
FROM
    order o
    JOIN customer c on
        o.customer_id = c.id
WHERE
    (o.id = ?) OR (c.name = ?)

使用ORMLite执行此操作的最佳方法是什么?

解决方法

ORMLite现在支持 simple join queries.

所以你的查询看起来像这样:

QueryBuilder<Customer,Integer> customerQb = customerDao.queryBuilder();
SelectArg nameSelectArg = new SelectArg();
// this gives the c.name = ?
customerQb.where().eq("name",nameSelectArg);
QueryBuilder<Account,Integer> orderQb = orderDao.queryBuilder();
SelectArg idSelectArg = new SelectArg();
// this gives the o.id = ?
orderQb.where().eq("id",idSelectArg);
orderQb.join(customerQb);
// then you set the args and run the query
nameSelectArg.setValue("jim");
idSelectArg.setValue(1);
List<Order> results = orderQb.join(customerQb).query();

猜你在找的Android相关文章