Need a join query in greendao

1.6k Views Asked by At

Can someone create a greendao (or android sqllite) query to have the same result like the next sql query?

select b.*, a.MAIN_CATEGORY_ID from MAINCATEGORYS_TO_LISTINGS a join APMAIN_CATEGORY b on b._id=a.MAIN_CATEGORY_ID where listing_id=10120

2

There are 2 best solutions below

0
On

This will be supported in greenDAO 1.4, which will be released soon. If want you can build greenDAO from the "join" branch for an early version of it: https://github.com/greenrobot/greenDAO/tree/join

0
On

You could use the queryRaw() method in GreenDao.

If I understand what you are trying to do correctly, e.g.:

session.getMainCategoryDao().queryRaw(
    " inner join " + MainCategoryToListingsDao.TABLENAME + " MCL " 
    + " on T._id = MCL." + MainCategoryToListingsDao.Properties.MainCategoryId.columnName 
    + " where MCL." + MainCategoryToListingsDao.Properties.ListingId.columnName 
    + " = ?", listing.getId());

It's a little ugly, but should work. Of course you will have to modify based on how your DAOs are named and possibly how you named your properties. But when GreenDao names your primary table in the query, it is aliased by the T and the primary keys are _id

They rest you can pull by using the DAO's properties.