Cannot insert json value with jooq-vertx-classic-reactive: org.jooq.JSON can not be coerced to the expected class java.lang.Object

Viewed 311

I have table with 3 columns and one of them has 'json' type. When I try to insert values(with empty json, but it doesnt' matter is it empty or not, it doesn't work in any way.) then I get the error:

java.lang.AssertionError: io.vertx.core.impl.NoStackTraceThrowable: Parameter at position[1] with class = [org.jooq.JSON] and value = ["{}"] can not be coerced to the expected class = [java.lang.Object] for encoding.

Here is my code:

@Override
  public Future<Void> saveInstances(List<Instances> instances, String tenantId) {
    return getQueryExecutor(tenantId).transaction(queryExecutor -> queryExecutor.execute(dslContext -> {
      InsertValuesStep3<InstancesRecord, UUID, JSON, String> insertValues = dslContext.insertInto(INSTANCES, INSTANCES.INSTANCE_ID,
          INSTANCES.JSON, INSTANCES.REQUEST_ID);
      instances.forEach(instance -> insertValues.values(instance.getInstanceId(), instance.getJson(), instance.getRequestId()));
      return insertValues;
    })
      .map(rows -> null));
  }

getQueryExecutor(tenantId) returns the ReactiveClassicGenericQueryExecutor object insatnce.getJson() reutns org.jooq.JSON type.

Database is PostgreSQL. I don't want to use any converters and etc. Documentation says that jooq supports JSON and jsonb natively but what is the problem with them here? As well I didn't get rowMappers generated for tables that contain JSON or jsonb columns.

<jooq.version>3.13.2</jooq.version>
<vertx-jooq.generator.version>5.1.1</vertx-jooq.generator.version>
<vertx-jooq.classic-reactive.version>5.2.0</vertx-jooq.classic-reactive.version>

Got confused with that, any suggestions will be helpful. Thank you!

1 Answers

I got the same error and solved it by adding to the pom.xml:

<configuration>
   <generator>
      <database>
         <forcedTypes>
            <forcedType>
               <userType>io.vertx.core.json.JsonObject</userType>
               <converter>io.github.jklingsporn.vertx.jooq.shared.postgres.JSONToJsonObjectConverter</converter>
               <expression>[column expression here]</expression>
               <types>(?i:JSON)</types>
            </forcedType>
         </forcedTypes>
      </database>
   </generator>
</configuration>

Replace JSONToJsonObjectConverter by JSONBToJsonObjectConverter, if you have JSONB column and specify the type for it. In expression you specify the columns for applying the specifc converter (e.g. only the column name).

This replaces the org.jooq.JSON type in your POJO's with io.vertx.core.json.JsonObject.

Be careful to choose compatible versions of vert.x and vertx-jooq.

Related