java - jOOQ can I fetch a join of two tables into the respective POJOs -
in jooq if want fetch row of table jooq autogenerated pojos do, instance:
dsl.selectfrom(user) .where(user.u_email.equal(email)) .fetchoptionalinto(user.class);
now, suppose want join between 2 tables, e.g. user
, role
, how can fetch result pojos these 2 tables?
this 1 solution using resultquery.fetchgroups(recordmapper, recordmapper)
map<userpojo, list<rolepojo>> result = dsl.select(user.fields()) .select(role.fields()) .from(user) .join(user_to_role).on(user.user_id.eq(user_to_role.user_id)) .join(role).on(role.role_id.eq(user_to_role.role_id)) .where(user.u_email.equal(email)) .fetchgroups( // map records first user table , key pojo type r -> r.into(user).into(userpojo.class), // map records first role table , value pojo type r -> r.into(role).into(rolepojo.class) );
note, if want use left join
instead (in case user not have roles, , want empty list per user), you'll have translate null
roles empty lists yourself.
make sure have activated generating equals()
, hashcode()
on pojos in order able put them in hashmap
keys:
<pojosequalsandhashcode>true</pojosequalsandhashcode>
Comments
Post a Comment