Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GreenDAO Left Join

Consider I have a model : Company 1 - N Person

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

I order to avoid that every call to person.getCompany() results in a new SQL query so I was thinking about adding a join :

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

The problem is that I only get the persons with a company because the generated query uses JOIN which is equivalent to INNER JOIN. I think I would need LEFT JOIN to also get the persons without a company.

It doesn't seem GreenDAO supports LEFT JOIN right now. Is there another way to make the request without doing a raw query ?

like image 276
Yohan D Avatar asked Aug 16 '26 18:08

Yohan D


1 Answers

For those who don't know how to do it with raw query:

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

Greendao will insert SELECT T.COLUMN_1, ... T.COLUM_N from PERSON AS T part by itself.
So in query T is just alias name for Person table.

like image 196
ashakirov Avatar answered Aug 19 '26 17:08

ashakirov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!