Named native query triggers proxy return type conflict with Hibernate 8.0.0-SNAPSHOT

Hello everyone,

I am currently experimenting with Hibernate 8 in integration with Spring Framework and Spring Data JPA, and I ran into an exception related to proxy creation when using a named native query.

I isolated the problem in a small standalone reproducer using:

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

With that profile, the call to EntityManager#createNamedQuery(...) fails with an IllegalArgumentException about methods with the same signature but incompatible return types during proxy generation.

So far I have been able to isolate the problem on a named native query case, but I cannot rule out that it may also occur in other combinations or with other query variants. This is simply the first scenario I was able to reduce in a clear and reproducible way.

Before investigating this only from the Spring side, I wanted to also ask here:
does this look like expected behavior from the Hibernate 8 side?
Do you have any suggestions on how to interpret it, or possible workarounds?

I also prepared a small standalone reproducer here:

  • oscarfanchin/test-spring-h8
  • https://github.com/oscarfanchin/test-spring-h8

The observed stack trace is:

java.lang.IllegalArgumentException: methods with same signature setProperties(java.lang.Object) 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)
	at java.base/java.lang.reflect.Proxy$ProxyBuilder.build(Proxy.java:578)
	at java.base/java.lang.reflect.Proxy.lambda$getProxyConstructor$1(Proxy.java:398)
	at java.base/jdk.internal.loader.AbstractClassLoaderValue$Memoizer.get(AbstractClassLoaderValue.java:329)
	at java.base/jdk.internal.loader.AbstractClassLoaderValue.computeIfAbsent(AbstractClassLoaderValue.java:205)
	at java.base/java.lang.reflect.Proxy.getProxyConstructor(Proxy.java:396)
	at java.base/java.lang.reflect.Proxy.newProxyInstance(Proxy.java:924)
	at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityHandlerInvocationHandler.invokeMethod(SharedEntityManagerCreator.java:264)
	at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:403)
	at jdk.proxy2/jdk.proxy2.$Proxy109.createNamedQuery(Unknown Source)
	at it.test.HibernateIntegrationH8Test.lambda$0(HibernateIntegrationH8Test.java:35)

If useful, I can also share the related Spring Framework issue I opened for the same reproducer.

This is clearly a Spring Framework problem. I suggest you try discussing this with the Spring folks instead. There is nothing we can do here.