whisperbye
whisperbye

Reputation: 75

Jooq: jsonb fetchInto custom type

what's the correct way to map jsonb record into custom data type? just like normal table fields.

create table t_order
(
    id    serial
        constraint t_order_pk
            primary key,
    key   varchar(255) not null,
    value jsonb        not null
);

create unique index t_order_id_uindex
    on t_order (id);

create index t_order_value_index
    on t_order using gin (value);
@Data
@Builder
class Order {

  private List<OrderItem> orderItems;

  @Data
  @Builder
  public static class OrderItem {

    private BigDecimal price;
    private BigDecimal quantity;
  }
}
@Test
  void findOrderById() {
    var order =
        dsl.select(T_ORDER.VALUE)
            .from(T_ORDER)
            .where(T_ORDER.ID.eq(1))
            .fetchOptionalInto(Order.class); // OOPS!
    assertTrue(order.isPresent());
    log.info(order);
}

Upvotes: 2

Views: 506

Answers (1)

Lukas Eder
Lukas Eder

Reputation: 221275

If you put gson or jackson on your classpath, jOOQ's DefaultConverterProvider will automatically use those to map your JSONB content to your data, so that should be all you need to do to make this work.

Upvotes: 1

Related Questions