Description When using Spring Data Jpa to write a query multiplying an entity field with a passed parameter hibernate seems to get confused which number should have which type, resulting in a CoercionException. Steps to reproduce
- Import attached project as gradle project into your IDE
- Run the tests
Expected: Tests pass (no asserts → no failures) Actual result: All tests throw the InvalidDataAccessApiUsageException caused by Hibernate throwing a CoercionException. Details Queries the like of
@Query("""
SELECT test.intField * ?1
FROM TestEntity test
""")
BigDecimal calculateFromEntityField(BigDecimal value);
throw an Exception with message
Parameter value [2.5] did not match expected type [BasicSqmPathSource(intField : Integer) ]
Note that 2.5 is passed as the value parameter and is of type BigDecimal. Attempting to use it for the entity's intField does not work, since it is not of type Integer. I initially suspected the error may be with Spring but when debugging I see they call Hibernate’s AbstractSharedSessionContract.createQuery with the query string SELECT test.intField*?1 FROM TestEntity test which seems correct. In return they get back an object of type QuerySqmImpl with the content querySqmImpl.parameterMetadata.queryParameters[0].key.anticipatedType = BasicSqmPathSource(intField : Integer) which does not match the input value of 2.5 and should presumably be some kind of BigDecimal datatype instead. So it seems to me that the issue probably lies with Hibernate getting confused which of the operands of the multiplication is the query parameter. Note that I tried to use named parameters and parameter objects (unwrapped using SPeL i.e. {{:# {#params.myParam} }} as well and always got the same result. |