Update: Hibernate bug HHH-17899 raised.

Environment: Spring Boot & Spring JPA 3.2.4, Hibernate 6.4.4, PostgreSQL.

I have a custom MonetaryAmount class, which internally wraps a javax.money.MonetaryAmount and a BigDecimal:

public class MonetaryAmount extends Number implements Comparable<MonetaryAmount> {
   ...

and I am using this as a JPA type, along with a MonetaryAmountConverter (which implements AttributeConverter<MonetaryAmount, BigDecimal>):

    @Convert(converter = MonetaryAmountConverter.class)
    @Column(name = "total_amount")
    private MonetaryAmount totalAmount = ZERO;

    @Convert(converter = MonetaryAmountConverter.class)
    @Column(name = "amount_raised")
    private MonetaryAmount amountRaised = ZERO;

The database columns in my Postgres database are of type numeric(20,2).

When I'm writing Spring JPA queries, if I use a mathematical operator +, -, * or / between the two above fields on the LHS, and a single numeric literal on the RHS like so:

@Query(value = "SELECT l FROM Loan l where l.amountRaised + l.totalAmount >= 2 ")

then I get the following Hibernate exception:

Caused by: org.hibernate.query.sqm.sql.ConversionException: Could not determine ValueMapping for SqmExpression: org.hibernate.query.sqm.tree.expression.SqmBinaryArithmetic@55bc8579
    at app//org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.determineValueMapping(BaseSqmToSqlAstConverter.java:5872)
    at app//org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.lambda$visitComparisonPredicate$111(BaseSqmToSqlAstConverter.java:7524)
at app//org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.visitHqlNumericLiteral(BaseSqmToSqlAstConverter.java:5583)
...

So essentially it is not able to parse the numeric type on the RHS (even 2.0 and 2BD do not work). But here is the weird thing: if I include a second numeric literal term on the RHS, e.g. multiply the first numeric literal by 1, then it works fine:

@Query(value = "SELECT l FROM Loan l where l.amountRaised + l.totalAmount >= 2 * 1")

Is this a Hibernate bug? Or do I need to change something in my custom class? You can find the test code here: https://github.com/sunildmonte/coffee.

0

There are 0 best solutions below