hibernate / hibernate-reactive

A reactive API for Hibernate ORM, supporting non-blocking database drivers and a reactive style of interaction with the database.
https://hibernate.org/reactive
Apache License 2.0
432 stars 90 forks source link

Bigdecimal cannot be coerced to expected class String in 2.4 #1984

Open nielsvhaldvb opened 1 week ago

nielsvhaldvb commented 1 week ago

Hi, an application of ours broke using Hibernate Reactive in Quarkus when updating quarkus to 3.14.1 and fixed by reverting to 3.13.3. Quarkus 3.14 started using Hibernate ORM 6.6 / Reactive 2.4.

org.hibernate.HibernateException: io.vertx.core.impl.NoStackTraceThrowable: Parameter at position[3] with class = [java.math.BigDecimal] and value = [200] can not be coerced to the expected class = [java.lang.String] for encoding

Usage: ( jakarta.persistence.criteria.CriteriaBuilder ) predicates.add( criteriaBuilder.between( criteriaBuilder .function( JSONB_EXTRACT_PATH, String.class, amountPath, criteriaBuilder.literal(AMOUNT)) .as(BigDecimal.class), filter.getAmountGe(), filter.getAmountLe()));

Are there any known issues within 6.6/2.4?

DavideD commented 1 week ago

Which database?

nielsvhaldvb commented 1 week ago

Postgres 15.4 in production environment.

Also does not work locally in a testcontainer with 16.1-alpine postgres image

DavideD commented 1 week ago

Would you be able to provide a test case?

DavideD commented 1 week ago

We test with BigDecimal and it seems to work fine: https://github.com/hibernate/hibernate-reactive/blob/80e4ed9ad247eb0b60b831d8d4bb9b2bcd224198/hibernate-reactive-core/src/test/java/org/hibernate/reactive/types/BasicTypesAndCallbacksForAllDBsTest.java#L363

But it seems you are using criteria and a function. So I don't know if there's something different compare to what we are doing.

DavideD commented 1 week ago

Actually, the test I linked doesn't run a query. So it might not really be relevant

DavideD commented 6 days ago

@nielsvhaldvb I've created this testcase and it seems to work.

Could you update it to make it look more similar to your case, please?

stefvanderweldevolksbanknl commented 2 days ago

@nielsvhaldvb I've created this testcase and it seems to work.

Could you update it to make it look more similar to your case, please?

Hi, thanks for your testcase! Could it have something to do with the property being within a jsonb type? I currently have connectivity issues with Gradle, so I am unable to make a commit. However, our Entity looks something like this:

    @Entity(name = "Book")
    @Table(name = BOOK_TABLE)
    static class Book {

        @Column(name = "price")
        @JdbcTypeCode(SqlTypes.JSON)
        @JdbcType(PostgreSQLJsonPGObjectJsonbType.class)
        Amount price;

        class Amount {
            BigDecimal amount;
        }
    }

We then use jsonb_extract_path_text() to query like this:

final var bookRoot = query.from(Book.class);

criteriaBuilder.between(
              criteriaBuilder
                  .function(
                      "jsonb_extract_path_text",
                      String.class,
                      bookRoot.get("price"),
                      criteriaBuilder.literal("amount"))
                  .as(BigDecimal.class),
              filter.getBookingAmountGe(),
              filter.getBookingAmountLe());