GreenDAO Left Join

I have a model: Company 1 - N people

I want to display a list of persons with their names and their company name. But not every person has a company.

I try to make every call to person.getCompany() lead to a new SQL query, so I was thinking of adding a connection:

 QueryBuilder<Person> queryBuilder = session.getPersonDao().queryBuilder(); queryBuilder.join(PersonDao.Properties.CompanyId, Company.class); queryBuilder.list() 

The problem is that I only get individuals with the company, because the generated request uses JOIN, which is equivalent to INNER JOIN. I think I will need a LEFT JOIN to also get people without company.

GreenDAO doesn't seem to be supporting LEFT JOIN right now. Is there any other way to make a request without executing a raw request?

+6
source share
2 answers

This can be achieved without writing a raw request.

Here is a snippet:

 QueryBuilder<Person> qb = userDao.queryBuilder(); List<Person> outputPersonList = userDao.queryDeep(" ", null); 

It compiles at a low level:

 SELECT T."_id",T."NAME",T."COMPANY_ID",T0."_id",T0."NAME" FROM PERSON T LEFT JOIN COMPANY T0 ON T."COMPANY_ID"=T0."_id" 

Of course, if the Person model has other relationships, they will all be extracted, so this SQL query will be slow.

However, this basically does what you expect.

0
source

For those who do not know how to do this with a raw request:

 String query = " LEFT JOIN COMPANY ON T.id = COMPANY.ID where SOME_CONDITIONS"; List<Person> persons = session.getPersonDao().queryRaw(query); 

Greendao will insert a SELECT T.COLUMN_1, ... T.COLUM_N from PERSON AS T part by itself.
Thus, in the T query, there is simply an alias for the Person table.

0
source

All Articles