Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Mapstruct as RecordMapper for JOOQ

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)

like image 351
emazzotta Avatar asked Sep 09 '26 19:09

emazzotta


1 Answers

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)
like image 195
Eric Riese Avatar answered Sep 11 '26 10:09

Eric Riese