1

the problem which I have is how to convert jooq select query to some object. If I use default jooq mapper, it works but all fields must be mentioned, and in exact order. If I use simple flat mapper, I have problems with multiset.

The problem with simple flat mapper:

class Student {
  private final id;
  Set<String> bookIds;
}

private static final SelectQueryMapper<Student> studentMapper = SelectQueryMapperFactory.newInstance().newMapper(Studen.class);

var students = studentMapper.asList(
    context.select(
       STUDENT.ID.as("id),
       multiset(
          select(BOOK.ID).from(BOOK).where(BOOK.STUDENT_ID.eq(STUDENT.ID)),
       ).convertFrom(r -> r.intoSet(BOOK.ID)).as("bookIds"))
       .from(STUDENT).where(STUDENT.ID.eq("<id>"))
)

Simple flat mapper for attribute bookIds returns: Set of exact one String ["[[book_id_1], [book_id_2]]"], instead of ["book_id_1", "book_id_2"]

As I already mention, this is working with default jooq mapper, but in my case all attributes are not mention in columns, and there is possibility that some attributes will be added which are not present in table.

The question is, is there any possibility to tell simple flat mapper that mapping is one on one (Set to set), or to have default jooq mapper which will ignore non-matching and disorder fields.

Also what is the best approach in this situations

krky
  • 11
  • 2

1 Answers1

0

Once you start using jOOQ's MULTISET and ad-hoc conversion capabilities, then I doubt you still need third parties like SimpleFlatMapper, which I don't think can deserialise jOOQ's internally generated JSON serialisation format (which is currently an array of arrays, not array of objects, but there's no specification for this format, and it might change in any version).

Just use ad-hoc converters.

If I use default jooq mapper, it works but all fields must be mentioned, and in exact order

You should see that as a feature, not a bug. It increases type safety and forces you to think about your exact projection, helping you avoid to project too much data (which will heavily slow down your queries!)

But you don't have to use the programmatic RecordMapper approach that is currently being advocated in the jOOQ manual and blog posts. The "old" reflective DefaultRecordMapper will continue to work, where you simply have to have matching column aliases / target type getters/setters/member names.

Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509