ormlite-android
ormlite-android copied to clipboard
how to join two table?
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"
}
}
-
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();
The short answer is that you can't without a lot of customization. Unfortunately, ORMLite right now does not support composite objects like this.