Skip to content

IllegalArgumentException when creating named native query via Shared EntityManager with Hibernate 8.0.0-SNAPSHOT / JPA 4.0.0-M4Β #36878

Closed

Description

@oscarfanchin

IllegalArgumentException when creating named native query via SharedEntityManager with Hibernate 8.0.0-SNAPSHOT / JPA 4.0.0-M4

Hello,

I am currently exploring Jakarta Persistence 4.0 support in the Spring Data JPA ecosystem and, while doing that, I ran into a problem that I tried to isolate in a minimal standalone reproducer.

To avoid interference from other layers as much as possible, I prepared a small reproduction project using only Spring Framework and Hibernate ORM, with the latest snapshot/milestone versions available to me. In particular, the problematic scenario occurs with:

  • Spring Framework 7.1.0-SNAPSHOT
  • Hibernate ORM 8.0.0-SNAPSHOT
  • Jakarta Persistence 4.0.0-M4

As a baseline for comparison, the same reproducer also includes a profile using earlier stable versions, where this behavior does not occur.

For the domain/test model, instead of creating an ad hoc example from scratch, I preferred to reuse the same entity class imported from Spring Data tests, so that I could start from an already realistic case and focus only on isolating the behavior.

At the moment I focused on the first scenario I was able to isolate clearly and reproduce consistently: creating a named native query through Spring's SharedEntityManager.

I cannot rule out that there may be other similar cases, but for now I preferred to start from this specific example and keep the reproducer as small and focused as possible.

In the reproducer:

  • with the baseline profile, the test passes and the query is created successfully;
  • with the profile using Hibernate 8 / JPA 4.0, the call to EntityManager#createNamedQuery(...) fails with an IllegalArgumentException.

The Hibernate 8 test is therefore also green, but only because it explicitly asserts the expected failure, i.e. the exception observed in this scenario, and prints the full stack trace to make inspection easier.

I collected everything in a small standalone reproduction repository so that the case is easy to run and, if needed, easy to extend with additional query variants or version combinations.

Reproducer

Repository: oscarfanchin/test-spring-h8

Steps to reproduce

Run the baseline profile:

mvn test

Run the Hibernate 8 / JPA 4.0 profile:

mvn test -Ph8-jpa4

Observed behavior

Under the Hibernate 8 / JPA 4.0 profile, calling:

em.createNamedQuery("User.findByNativeNamedQueryWithPageable")

fails with an IllegalArgumentException.

java.lang.IllegalArgumentException: methods with same signature setProperties(java.util.Map) but incompatible return types: [interface org.hibernate.query.MutationOrSelectionQuery, interface org.hibernate.query.sql.spi.NativeQueryImplementor] at java.base/java.lang.reflect.ProxyGenerator.checkReturnTypes(ProxyGenerator.java:332) at java.base/java.lang.reflect.ProxyGenerator.generateClassFile(ProxyGenerator.java:467) at java.base/java.lang.reflect.ProxyGenerator.generateProxyClass(ProxyGenerator.java:211) at java.base/java.lang.reflect.Proxy$ProxyBuilder.defineProxyClass(Proxy.java:469)

Expected behavior

I would expect the named native query to be created successfully, as it is with the baseline profile, or at least I would like to understand whether this is a known incompatibility in Spring's SharedEntityManager integration/proxying for the current Hibernate 8 / Jakarta Persistence 4.0 API shape.

Notes

This reproducer currently focuses only on this named native query case because it is the first failing scenario I was able to isolate clearly. If useful, I can extend it with additional query variants to verify whether the issue is broader than this specific case.

Activity

  1. self-assigned this
    on Jun 9, 2026
  2. added
    in: dataIssues in data modules (jdbc, orm, oxm, tx)
    and removed on Jun 9, 2026
  3. added this to the 7.0.9 milestone on Jun 9, 2026
  4. added a commit that references this issue on Jun 9, 2026
    0b5a9ea
  5. jhoeller commented on Jun 9, 2026

    @jhoeller
    Contributor

    This turned out to be a problem with overlapping methods in various Hibernate Query interfaces, declaring a covariant return type for method chaining: specifically, MutationOrSelectionQuery and NativeQuery both declare the same methods with a different covariant return type that is only fully resolved in NativeMutationOrSelectionQueryImpl - which we cannot proxy since it is a concrete class rather than an interface. Unfortunately we cannot proxy the combination of interfaces either if some such covariant return type is not fully resolved in any of them.

    As a solution, we fall back to the declared Query interface from the EntityManager method return type for a start - and if any other Query type is needed on asStatement or unwrap calls, we dynamically adapt the query proxy to the expected query interface type (effectively creating a new proxy with the same InvocationHandler but a different proxy interface).

    Fixed for 7.0.9 (where we aim for initial runtime compatibility with Hibernate 8.0 pre-releases) as well as 7.1.0-M1 (where we aim for complete JPA 4.0 programming model support).

  6. oscarfanchin commented on Jun 11, 2026

    @oscarfanchin
    Author

    Hi, I tested my local Spring Data build, and all the tests are green. Thanks.

  7. marvin-hm commented on Aug 24, 2026

    @marvin-hm

    Hi @jhoeller,

    we're facing an issue which seems to be related to this change. We're creating a stored procedure and getting the exception

    java.lang.IllegalArgumentException: OUT/INOUT parameter not available: 1
    
    	at org.springframework.orm.jpa.SharedEntityManagerCreator$DeferredQueryInvocationHandler.invoke(SharedEntityManagerCreator.java:554)
    

    As far as I can see, the issue is related to the change of this issue, within the method Object invoke(Object proxy, Method method, @Nullable Object[] args) throws Throwable. It seems like the else-if block which checks for registerStoredProcedureParameter is never reached, when calling the method StoredProcedureQuery registerStoredProcedureParameter(int position, Class<?> type, ParameterMode mode); from a proxy created via entityManager.createStoredProcedureQuery(procedureName);. That leads to a map of outputParameters which is never filled and to the exception.

    I haven't found any related issue so far and we've just updated from Spring Boot 4.1.0 to 4.1.1 (spring-orm 7.0.8 -> 7.0.9). Could you check that, please?

    Thanks and BR

    Marvin

  8. sbrannen commented on Aug 24, 2026

    @sbrannen
    Member

    Hi @marvin-hm,

    Since this issue is closed and 7.0.9 has already been released, if you think you have encountered a regression, please create a new issue with the details and a small application (or test class) which reproduces the issue -- preferably something we can download and run such as a sample app or test in a public Git repository or ZIP file.

    Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: dataIssues in data modules (jdbc, orm, oxm, tx)type: bugA general bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions