我在 android 中使用 ormlite。图书馆似乎很有帮助,但我对 QueryBuilder 有疑问。我将尝试用一些抽象的例子来解释这一点。假设我们有 4 个表:Projects、Employees、Country、ActiveProject。单个项目可以有多个员工,但员工只能在一个项目上工作。员工来自一个国家。ActiveProject 表有一行表示当前活动的项目。
现在我想列出来自指定国家(“en”)的所有员工,这些员工为当前活动的项目工作。
//retrieving Daos:
Dao<Projects, Long> projectDao = helper.getDao(Projects.class);
Dao<Employees, Long> employeeDao = helper.getDao(Employees.class);
Dao<Countries, Long> countryDao = helper.getDao(Countries.class);
Dao<ActiveProject, Long> activeProjectDao = helper.getDao(ActiveProject.class);
//retrieving QueryBuilder
projectQb = projectDao.queryBuilder();
employeeQb = employeeDao.queryBuilder();
countryQb = countryDao.queryBuilder();
activeProjectQb = activeProjectDao.queryBuilder();
//constructing query:
//add WHERE statement for country
countryQb.where().eq(Countries.NAME,"en");
employeeQb.join(countryQb);
//so far so good, when I query from employees here, I'll get all employees from "en"
//limit the results to active project only
projectQb.join(activeProjectQb);
//if I query the projectQb for all projects, I'll get only the active one - as expected.
//but here the problem starts
employeeQb.join(projectQb);
现在,employeeQb.query() 将列出属于活动项目的所有员工,但来自所有国家。“Employees-Country join”和“WHERE”语句已经消失。
我在这里做错了吗?