问题
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());
}
回答1:
If you want to show data from both tables, you can use greenDAO relations. In your case, you will need a 1:1 relation between Employee
and Company
. So, greenDAO will create a Employee
class with a member of type Company
and when you load a Employee
, greenDAO will perform the join query and load the related company.
The new generator code:
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]);
}
}
Getting the employees of company with company_id = 1
:
QueryBuilder<Employee> queryBuilder = getEmployeeDao(context).queryBuilder();
List<Employee> employees = qb.where(EmployeeDao.Properties.Company_id.eq(1)).list();
Iterating through employees:
foreach(Employee employee : employees){
Company company = employee.getCompany();
String companyAddress = company.getComp_address();
//...
}
For more info, check greeenDAO Relations Docs.
来源:https://stackoverflow.com/questions/38315642/how-to-use-join-query-for-displaying-data-from-multiple-tables-in-greendao