我正在尝试使用ORMLite编写查询。我需要此查询检查其他实体中的自定义ID。我怎么能这样做?
实体
@DatabaseTable(tableName = "custommer")
public class Custommer{
@DatabaseField(generatedId = true)
private Integer id;
@DatabaseField
private String name;
@DatabaseField
private Sale sale;
//gets sets
}
@DatabaseTable(tableName = "sale")
public class Sale{
@DatabaseField(generatedId = true)
private Integer id;
@DatabaseField
private Custommer custommer;
@DatabaseField
private Integer status;
//gets sets
}
查询
Custommer custommer = new Custommer();
custommer.setId(1);
custommer.setName("Fernando Paiva");
QueryBuilder<Sale, Integer> qb = saleDAO.queryBuilder();
Where where = qb.where();
where.eq("sale.custommer.id", custommer.getId());
where.and();
where.eq("sale.status", 1);
PreparedQuery<Sale> pq = qb.prepare();
List<Sale> list = saleDAO.query(pq);
Log.i("SALE LIST->", list.size() + "");
答案 0 :(得分:1)
您需要使用JOIN
这里使用Join的示例:
这里是代码
Dao<Sale, Integer> saleDao = DaoManager.createDao(getConnectionSource(), Sale.class);
Dao<Custommer, Integer> custommerDao = DaoManager.createDao(getConnectionSource(), Custommer.class);
QueryBuilder<Sale, Integer> saleQa= saleDao.queryBuilder();
saleQa.where().eq("status", 1);
QueryBuilder<Custommer, Integer> custommerQa = custommerDao.queryBuilder();
custommerQa.where().idEq(custommer.getId());
sales = saleQa.join(custommerQa).query();
答案 1 :(得分:0)
您是否尝试使用OrmLite检查客户ID是否与销售ID相同并获得所有匹配结果?如果是这样,以下代码将执行该操作
qb.where().eq("id", custommer.id);
List<Sale> results = saleDAO.query(qb.prepare());
更新
在重读了你的问题后,我意识到你正在尝试做什么
qb.where().in(Sale.custommer, id);
有关详细信息,请参阅此问题。 Ormlite Foreign Entity Searching