Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JOOQ - convert result into Pojo

Tags:

java

sql

jooq

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.

like image 280
ascott Avatar asked Jan 10 '23 18:01

ascott


2 Answers

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);
like image 124
Anton Dozortsev Avatar answered Jan 18 '23 17:01

Anton Dozortsev


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);
like image 21
Tal Joffe Avatar answered Jan 18 '23 16:01

Tal Joffe