Following is my DAOgenerator class:
public class MyDaoGenerator {
public static void main(String args[]) throws Exception {
Schema schema = new Schema(3, "Dao");
Entity employee = schema.addEntity("Employee");
employee.addIdProperty().autoincrement();
employee.addStringProperty("name");
employee.addStringProperty("mobile");
employee.addStringProperty("address");
employee.addStringProperty("company_id");
Entity company = schema.addEntity("Company");
company.addIdProperty().autoincrement();
company.addStringProperty("comp_name");
company.addStringProperty("comp_location");
company.addStringProperty("comp_address");
new DaoGenerator().generateAll(schema, args[0]);
}
}
Following is my DAO queries class.
This class has all the function like insert
, delete
, clear table
:
public class DaoQueries {
private static CompanyDao getMyCompanyDao(Context context){
return ((AppMain)context.getApplicationContext()).getDaoSession().getCompanyDao();
}
public static void AddCompany(Context context,Company company){
getMyCompanyDao(context).insertOrReplace(company);
}
public static int companySize(Context context){
return getMyCompanyDao(context).loadAll().size();
}
public static List<Company> getCompanies(Context context){
return getMyCompanyDao(context).loadAll();
}
private static EmployeeDao getEmployeeDao(Context context){
return ((AppMain)context.getApplicationContext()).getDaoSession().getEmployeeDao();
}
public static void AddEmployee(Context context,Employee employee){
getEmployeeDao(context).insertOrReplace(employee);
}
public static List<Employee> getEmployees(Context context){
return getEmployeeDao(context).loadAll();
}
public static void joinedUser(Context context){
QueryBuilder<Employee> queryBuilder = getEmployeeDao(context).queryBuilder();
queryBuilder.join(Company.class,CompanyDao.Properties.Id)
.where(CompanyDao.Properties.Id.eq(1));
List<Employee> employees = queryBuilder.list();
Log.e("DAO","EMPLOYE JOIN "+employees.size());
}
}
I tried join query with greenDAO as mentioned in dao documentation for getting rows from both the tables like this:
public static void joinedUser(Context context){
QueryBuilder<Employee> queryBuilder = getEmployeeDao(context).queryBuilder();
queryBuilder.join(Company.class,CompanyDao.Properties.Id)
.where(CompanyDao.Properties.Id.eq(1));
List<Employee> employees = queryBuilder.list();
Log.e("DAO","EMPLOYE JOIN "+employees.size());
}
答案 0 :(得分:1)
如果要显示两个表中的数据,可以使用greenDAO关系。在您的情况下,Employee
和Company
之间需要1:1的关系。因此,greenDAO将创建一个Employee
类,其成员类型为Company
,当您加载Employee
时,greenDAO将执行加入查询并加载相关公司。
新的生成器代码:
public class MyDaoGenerator {
public static void main(String args[]) throws Exception {
Schema schema = new Schema(3, "Dao");
Entity company = schema.addEntity("Company");
company.addIdProperty().autoincrement().notNull();
company.addStringProperty("comp_name");
company.addStringProperty("comp_location");
company.addStringProperty("comp_address");
Entity employee = schema.addEntity("Employee");
employee.addIdProperty().autoincrement();
employee.addStringProperty("name");
employee.addStringProperty("mobile");
employee.addStringProperty("address");
Property companyId = employee.addLongProperty("company_id").getProperty();
employee.addToOne(company, companyId);
new DaoGenerator().generateAll(schema, args[0]);
}
}
通过company_id = 1
QueryBuilder<Employee> queryBuilder = getEmployeeDao(context).queryBuilder();
List<Employee> employees = qb.where(EmployeeDao.Properties.Company_id.eq(1)).list();
通过员工进行迭代:
foreach(Employee employee : employees){
Company company = employee.getCompany();
String companyAddress = company.getComp_address();
//...
}
有关详情,请查看greeenDAO Relations Docs。