我刚刚开始阅读ORMLite,所以我还是初学者。 据我所知,我可以使用任何持久属性查询对象。
例如,如果我有以下类:
@DatabaseTable
public class Bill {
@DatabaseField String code;
Client client;
List<Item> items;
}
@DatabaseTable
class Client {
@DatabaseField String name;
}
@DatabaseTable
class Item {
@DatabaseField String name;
}
(不确定如何在类client
中注释items
和Bill
属性。)
在任何情况下,像这样的查询将帮助我获得具有特定代码编号的所有Bill对象:
QueryBuilder<Bill, String> queryBuilder = BillDao.queryBuilder();
Where<Bill, String> where = queryBuilder.where();
where.eq(BILL.NAME_CODE, "abc123");
PreparedQuery<Account> preparedQuery = queryBuilder.prepare();
我的问题是:ORMLite建议在模型对象的传递关系中编写具有条件的查询的方法是什么?例如“包含具有特定名称的特定项目的所有账单”?或“所有购买了具有特定名称的物品的客户”?。
提前致谢!
答案 0 :(得分:1)
读取外部对象字段和Foreign Collections in ormLite docs。
您的关系将是:
@DatabaseTable(tableName = "bills")
public class Bill {
public static final String ACCOUNT_ID_FIELD_NAME = "client_id";
@DatabaseField(generatedId = true)
private int id;
@DatabaseField(foreign = true, columnName = ACCOUNT_ID_FIELD_NAME)
private Client client;
@DatabaseField
private String code;
Client client;
List<Item> items;
public Bill(){
}
public Bill(Client client, String code){
this.client = client;
this.code = code;
}
public int getId(){
return this.id;
}
public String getCode(){
return this.code;
}
}
@DatabaseTable(tableName = "clients")
class Client {
public static final String NAME_FIELD_NAME = "name";
public Client() {
// all persisted classes must define a no-arg constructor with at least package visibility
}
public Client(String name) {
this.name = name;
}
@DatabaseField(generatedId = true)
private int id;
@DatabaseField(columnName = NAME_FIELD_NAME, canBeNull = false)
private String name;
public int getId(){
return this.id;
}
public String getName(){
return this.name;
}
}
更多信息可在code examples。
中找到