Home > Back-end >  Return custom JOOQ record when joining multiple table and selecting omonimous fields
Return custom JOOQ record when joining multiple table and selecting omonimous fields

Time:11-19

I'm trying to make a little method to recicle the code related to retrieve some data to make it easy to use i need to make the resulting table records accessible, so i tried to make a CustomTable with a CustomRecord.

to make more clear what i'm trying to do i write down a little sample as what i tried:

public MyCustomTable getTableReusable(final DSLContext ctx,
    final Profile profile, final LocalDate date) {
    final Table<Table1Record> tab1 = Table1.TABLE1.as("a");
    final Table<Table2Record> tab2 = tab2.TABLE2.as("b");
    
    return ctx.select(table1.field(tab1.ID).as(MyCustomTable.TAB1_ID), 
            tab1.field(Table1.A_FIELD).as(MyCustomTable.TAB1_A_FIELD), 
            tab2.field(Table2.ID).as(MyCustomTable.TAB2_ID), 
            tab2.field(Table2.B_FIELD).as(MyCustomTable.TAB2_B_FIELD)) 
            .from(tab1) 
            .leftJoin(tab2) 
            .on(tab1.field(Table1.ID).eq(tab2.field(Table2.TAB1_FK))) //
            .asTable();}

At the time it's impossible to cast this table to the custom table with all the field setted in the select method. In custom table i even make the CustomRecord with all the record exposed.

Exist a way to make me return the data in my customTable?

CodePudding user response:

I'm assuming you'd liek to have an object that behaves like any other generated table, but have it correspond to a dynamically constructed jOOQ query. A view expressed in jOOQ, if you like.

There's currently no easy way to achieve that. Pending feature requests include:

  • #1969 Add support for views expressed in jOOQ
  • #11054 Add code generator support for synthetic views

For now, you have to return a Table<?> instance, which lacks type safety, or just implement your view or table valued function directly in SQL and let the jOOQ code generator pick it up.

  • Related