JOOQ - displaying a single record with RecordMapper

I cannot use RecordMapper mapper

to map a single record obtained with fetchOne / fetchAny.

If I use for example (for example in the manual)

List<Something> list =  create.selectFrom(TABLE).fetch().map(mapper);

      

it works,

but if i use the same display tool for:

Something pojo = create.selectFrom(TABLE).fetchOne().map(mapper);

      

it doesn't compile.

thanks for the help

+3


source to share


2 answers


This is probably related to generics. While it Result.map()

accepts the type of argument RecordMapper<? super R,E>

, Record.map()

accept RecordMapper<Record,E>

, because it Record

does not have a recursive definition of a common type R

.

In other words, if you want to reuse the same mapper for, Result.map()

and Record.map()

unfortunately, you have to do it RecordMapper<Record, E>

, which means you lose some of the type safety on the record type.



A workaround would be to create a new mapping-only result:

Result<TableRecord> result = DSL.using(configuration).newResult(TABLE);
result.add(record);
Something pojo = result.map(mapper).get(0);

      

+2


source


An alternative workaround for Lukas Eder could be to use the translator itself:



TableRecord record = create.selectFrom(TABLE).fetchOne();    
Something pojo = mapper.map(record);

      

+1


source







All Articles