mat_boy
mat_boy

Reputation: 13666

jOOQ and selecting an enum field into enum object

I'm using jOOQ with Postgresql to select an enum value from a table.

List<my.project.jooq.enums.Color> colors =
dsl.selectDistinct(TABLE.T_COLOR.as("color"))
   .from(TABLE).fetch()
   .into(my.project.jooq.enums.Color.class);

Anyway I get the exception:

org.jooq.exception.MappingException: No matching constructor found on type class my.project.jooq.enums.Color for record org.jooq.impl.DefaultRecordMapper@7c66447f

I see that fetch() will return Result<Record1<my.project.model.jooq.enums.Color>>, so I wonder if there is a way to immediately fetch the Color enums into a list as I can do with any pojo.

How can I fetch into the enum values?

Upvotes: 1

Views: 4646

Answers (1)

Lukas Eder
Lukas Eder

Reputation: 220842

As of jOOQ 3.7, this is not yet supported out of the box, but will be in jOOQ 3.8 (see #5154).

You can easily map a string column to your Enum type yourself, though, via

List<my.project.jooq.enums.Color> colors = 
dsl.selectDistinct(TABLE.T_COLOR)
   .from(TABLE)
   .fetch()
   .map(rec -> my.project.jooq.enums.Color.valueOf(rec.getValue(TABLE.T_COLOR)));

In case, your my.project.jooq.enums.Color was generated by jOOQ from a PostgreSQL enum data type, you don't need to specifically map anything, jOOQ will automatically do that for you:

List<my.project.jooq.enums.Color> colors = 
dsl.selectDistinct(TABLE.T_COLOR)
   .from(TABLE)
   .fetch(TABLE.T_COLOR);

Upvotes: 2

Related Questions