jOOQ我可以将两个表的连接提取到相应的POJO中

在jOOQ中如果我想将一行表取出到jOOQ自动生成的POJO中,例如:

dsl.selectFrom(USER) .where(USER.U_EMAIL.equal(email)) .fetchOptionalInto(User.class); 

现在,假设我想在两个表之间进行连接,例如USERROLE ,如何将结果提取到这两个表的POJO中?

这是一个使用ResultQuery.fetchGroups(RecordMapper, RecordMapper)解决方案

 Map> 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 into the USER table and then into the key POJO type r -> r.into(USER).into(UserPojo.class), // Map records first into the ROLE table and then into the value POJO type r -> r.into(ROLE).into(RolePojo.class) ); 

请注意,如果您想使用LEFT JOIN (如果用户不一定有任何角色,并且您希望每个用户获得一个空列表),则必须自己将NULL角色转换为空列表。

确保已在POJO上激活生成equals()hashCode() ,以便能够将它们作为键放在HashMap中:

 true