map into DTO derived from record

213 views
Skip to first unread message

tod...@googlemail.com

unread,
Apr 5, 2023, 5:25:38 PM4/5/23
to jOOQ User Group
I stumble from time to time over the following problem: I want to map the result of a query into a DTO. The DTO should be derived from an updateble record generated by the code generator and extended by a few additional attributes.

public class MyDto extends MyTableRecord {
  private int additionalAttribute1;
}

With the query
jooq.select(MyTableRecord.fields())
.select(ANOTHER_TABLE.ANOTHER_FIELD.as("additionalAttribute1")
.join(ANOTHER_TABLE).on(...)
.fetchInto(MyDto.class)

only the properties defined in the MyTableRecord are mapped and returned, not the additionalAttribute1 ? Why is this and how can I solve this ?

Kind regards
Dominik

Lukas Eder

unread,
Apr 6, 2023, 4:08:03 AM4/6/23
to jooq...@googlegroups.com
Thanks for your message.

A Record's row type isn't defined by the attributes it contains. Imagine if that were the case, would we have to map to jOOQ's internal org.jooq.impl.AbstractRecord.changed or other properties, for example? No, an org.jooq.Record extends org.jooq.Fields, and thus defines exactly what fields are contained in it. As such, you cannot extend generated records and add more fields to them.

The idea looks hackish anyway. Why extend something that is well defined, rather than use composition? Why not write:

public class MyDto {
    // Or, whatever:
    MyTableRecord theRecord;
    int additionalAttribute1;
}

Doesn't that look much cleaner?

--
You received this message because you are subscribed to the Google Groups "jOOQ User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jooq-user/ac029a24-ce7b-4d76-931f-cc0b486e636dn%40googlegroups.com.

tod...@googlemail.com

unread,
Apr 7, 2023, 3:41:24 PM4/7/23
to jOOQ User Group
Hi Lukas,

thanks for your response.
I already use your approach but with pojos and call it ...

// composition of jooq pojos
public class VerwaltungseinheitFullDto extends Verwaltungseinheit {
   private Objekt objekt;
   private Wirtschaftseinheit wirtschaftseinheit;
   private Quartier quartier;
   private Stadtteil stadtteil;
}

The problem is here how to efficiently map the database result set into that composition class without manually handle every field/attribute ?
Sometimes I use org.modelmapper for that
ModelMapper modelMapper = new ModelMapper();
modelMapper.getConfiguration().addValueReader(new RecordValueReader());
modelMapper.getConfiguration().setSourceNameTokenizer(NameTokenizers.UNDERSCORE);

I put the value of the composition dto attribute name plus the underscore as prefix for every field
var objektFields = Arrays.asList(VERWALTUNGSEINHEIT.objekt().fields()).stream().map(field -> field.as("objekt_" + field.getName())).toList();

and let the modelmapper do his job
var result = modelMapper.map(dbResult, VerwaltungseinheitFullDto.class);

But its quite slow and often I prefer the updateable records in order to store() the record directly back ....

So I didn't found a way to map such DTO like yours. RecordMapper, RecordHandler or something like this seems not appropriate at least in this way:

.fetch(new RecordHandler<KalkulationPositionRecord, CompositionDto>() {
@Override
public CompositionDto map(KalkulationPositionRecord position) {
CompositionDto result = new CompositionDto();
result.setDbRecord(position);
return result;
}
});

But I'm quite sure, you have a way to map that without handle every field manually... :-)

kind regards
Dominik

Lukas Eder

unread,
Apr 11, 2023, 5:22:44 AM4/11/23
to jooq...@googlegroups.com
Hi Dominik,

I can't comment on ModelMapper. I don't think it's necessary to use third party mapping tools with jOOQ, at least not since jOOQ 3.15, when jOOQ added support for nested collections, nested records, and ad-hoc conversion. Using those features, do you still have any open questions? Here's a quick overview:

Best Regards,
Lukas

--
You received this message because you are subscribed to the Google Groups "jOOQ User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages