ormlite-android icon indicating copy to clipboard operation
ormlite-android copied to clipboard

how to join two table?

Open iobsessu opened this issue 2 years ago • 2 comments

now, i have a User object and a Role object , it look like this:

class User {
    private int id;
    private String username;
    private int roleId;
}
class Role {
    private int id;
    private String roleName;
}

i wanna know, how can i serialize it to

 {
    id: 10,
    name: "Jack",
    roleId: 20,
    roleName: "admin"
}

instead of

{
    id: 10,
    name: "Jack,
    role: {
        id: 20,
        roleName: "admin"
    }
}

iobsessu avatar Apr 21 '22 16:04 iobsessu

  • First, u should implement your raw sql code like below

  • Second, u implement the multi-table join query with QueryBuilder

  • First step

SELECT
	n.id ,
	n.title ,
	ti.tagName 
from
	TableN n
join TTableN twn on
	twn.note_id = n.id
join TTableI ti on
	ti.id = twn.tagInfo_id 
where ti.tagName ="customize1"  limit 1 offset 2;
  • Second step
        QueryBuilder<TableN, Long> tableNQuery = DatabaseUtils.initialiseDaoWithLong(TableN.class).queryBuilder();
        QueryBuilder<TTableN , Long> ttableNQuery = DatabaseUtils.initialiseDaoWithLong(TTableN .class).queryBuilder();
        QueryBuilder<TTableI , Long> ttableIQuery = DatabaseUtils.initialiseDaoWithLong(TTableI .class).queryBuilder();
 
        ttableNQuery .join(ttableIQuery );

        tableNQuery .join(ttableNQuery );
        tableNQuery .distinct()
        .offset(Const.Note.PAGE_SIZE * (pageNo - 1)).limit(Const.Note.PAGE_SIZE)
        .orderBy("updateTime", false)
        .where().eq("inTrash", false)
        .and().eq("stuId", stuId)
        .query();

pengcao1 avatar Jul 14 '22 02:07 pengcao1

The short answer is that you can't without a lot of customization. Unfortunately, ORMLite right now does not support composite objects like this.

j256 avatar Jul 15 '22 20:07 j256