I would like to implement my own RecordMapper and use Mapstruct to map the Record to the POJO. I don't quite understand how to accomplish this. I followed this part of the docs: https://www.jooq.org/doc/3.13/manual/sql-execution/fetching/pojos-with-recordmapper-provider/
My mapper looks like this:
public class LanguageMapper<R extends Record, E> implements RecordMapper<R, Language> {
@Override
public Language map(R record) {
LanguageRecord languageRecord = (LanguageRecord) record;
// this is just an example, in the future this is the kind of mapping that would be performed automatically via mapstruct
return new Language(
languageRecord.getId(),
languageRecord.getNamespaceId(),
languageRecord.getLanguage(),
languageRecord.getCountryCode(),
languageRecord.getLanguageTag()
);
}
}
The issue is that as a record I'm not actually getting a LanguageRecord but a RecordImpl of my language table and can thus not cast record to LanguageRecord. Any idea what I need to change?
What's interesting when using the RecordImpl is, if I do something like this
record.get(LANGUAGE.LANGUAGE_TAG);
It will already get the wrong information (it's getting the LANGUAGE.NAMESPACE_ID). Thus when getting it like this and then mapping it to the POJO it will be wrong as well.
(Created this question based on this question POJO Mapping in JOOQ regardless of parameter order)
I generated my Record classes and POJOs with jOOQ codegen and my minimal mapper just worked
@Mapper
public interface FooMapper extends RecordMapper<FooRecord, Foo>, RecordUnmapper<Foo, FooRecord> {
}
It warns about unmapped fields, but you can suppress that with.
@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With