Ruben Gehring (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=6401f5e...
) *created* an issue
Hibernate ORM (
https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiMTZjYTlmNDU0...
) / Bug (
https://hibernate.atlassian.net/browse/HHH-16986?atlOrigin=eyJpIjoiMTZjYT...
) HHH-16986 (
https://hibernate.atlassian.net/browse/HHH-16986?atlOrigin=eyJpIjoiMTZjYT...
) CoercionException caused by attempted coercion of query param to entity field type (
https://hibernate.atlassian.net/browse/HHH-16986?atlOrigin=eyJpIjoiMTZjYT...
)
Issue Type: Bug Affects Versions: 6.2.6 Assignee: Unassigned Attachments:
hibernate-issues.zip Components: query-hql Created: 24/Jul/2023 09:55 AM Environment:
Hibernate: 6.2.6
Java: 19 (Adoptium JDK)
OS: Windows 10
Database: H2 2.1.214
Spring Boot: 3.1.2 Priority: Major Reporter: Ruben Gehring (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=6401f5e...
)
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.
(
https://hibernate.atlassian.net/browse/HHH-16986#add-comment?atlOrigin=ey...
) Add Comment (
https://hibernate.atlassian.net/browse/HHH-16986#add-comment?atlOrigin=ey...
)
Get Jira notifications on your phone! Download the Jira Cloud app for Android (
https://play.google.com/store/apps/details?id=com.atlassian.android.jira....
) or iOS (
https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=Em...
) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100232- sha1:a133497 )