res1
res1

Reputation: 3660

JOOQ - map a single record using RecordMapper

I am not able to use a RecordMapper mapper to map a single record retrieved via fetchOne/fetchAny.

If I use for example (like in the manual)

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

it works,

but if i use the same mapper for:

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

it doesn't compile.

Thanks for the help

Upvotes: 4

Views: 6387

Answers (2)

gerferra
gerferra

Reputation: 1519

An alternative workaround to the one provided by Lukas Eder could be to use the mapper directly:

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

Upvotes: 1

Lukas Eder
Lukas Eder

Reputation: 221275

This probably has to do with generics. While Result.map() takes a RecordMapper<? super R,E> argument type, Record.map() takes RecordMapper<Record,E>, because Record has no recursive generic type definition for R.

In other words, if you want to reuse the same mapper for both Result.map() and Record.map(), unfortunately, you will need to make it a RecordMapper<Record, E>, which means you'll lose some of the type safety on the record type.

A workaround would be to create a new result just for the sake of mapping:

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

Upvotes: 2

Related Questions