简体   繁体   中英

JOOQ - convert result into Pojo

I have seen that JOOQ can automatically return a POJO when we use .selectFrom(TABLE) or .fetchInto(POJO.class);

But is it possible to convert the result of a complex query into multiple POJO ?

Example :

This query will return an array of all columns into tables Support and Box. It is possible to convert them into a Support and Box Pojo ?

Result<Record> results = query.select()
                         .from(BOX)
                         .join(SUPPORT)
                         .on(SUPPORT.ID.equal(BOX.SUPPORT_ID))
                         .where(SUPPORT.ID.equal("XXXX"))
                         .orderBy(BOX.ID)
                         .fetch();

I have tested the method .intoGroups(SUPPORT.ID, Box.class) , it works fine. But I doesn't have the support object.

Instantiate to SelectSeekStep1

With aliases it's more convenient:

Box b = BOX.as("b");
Support s = SUPPORT.as("s");

SelectSeekStep1<Integer, Integer> sql = query.select(b.ID, s.ID /* other columns */)
     .from(b)
     .join(s)
        .on(s.ID.eq(b.SUPPORT_ID))
     .where(s.ID.eq("XXXX"))
     .orderBy(b.ID)
;

Then just fetch what/as you need:

List<BoxRecord> boxes = sql.fetchInto(BOX); 
SupportRecord support = sql.limit(1).fetchOneInto(SUPPORT);

For future readers, if you want to achieve the same behaviour with insert methods you should use:

insertInto(BOX)
    .set(BOX.COLUMN1, UInteger.valueOf(1))
    .set(BOX.COLUMN2, "test")
    .returning()
    .fetchOne()
    .into(<POJO_class>.class);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM