addNamedQuery leads to "Named query exists, but did not specify a resultClass"
Description
Activity
Show:
Yoann RodièreDecember 14, 2023 at 1:59 PM
Yoann RodièreDecember 14, 2023 at 1:57 PM
May be related: if you use createNamedQuery(String)
instead of createNamedQuery(String, Class)
, the result type when executing doesn’t match the definition:
final Query query = entityManager.createNativeQuery(
"select g.* from Game g where title = ?", Game.class );
entityManagerFactory().addNamedQuery( "the-query", query );
final Query namedQuery = entityManager.createNamedQuery( "the-query" );
namedQuery.setParameter( 1, "Halo" );
assertThat( (List<?>) namedQuery.getResultList() )
.hasSize( 1 )
.element( 0 )
.asInstanceOf( InstanceOfAssertFactories.type( Game.class ) )
.returns( "Halo", Game::getTitle );
Exception:
java.lang.AssertionError: [List element at index 0]
Expecting actual:
[1L, "Halo"]
to be an instance of:
org.hibernate.orm.test.jpa.query.NamedQueryTest.Game
but was instance of:
java.lang.Object[]
at org.hibernate.orm.test.jpa.query.NamedQueryTest.lambda$testNamedQueryAddedFromEntityNativeQueryUsedAsUntyped$30(NamedQueryTest.java:270)
at org.hibernate.testing.transaction.TransactionUtil.doInJPA(TransactionUtil.java:260)
at org.hibernate.testing.transaction.TransactionUtil.doInJPA(TransactionUtil.java:301)
at org.hibernate.orm.test.jpa.query.NamedQueryTest.testNamedQueryAddedFromEntityNativeQueryUsedAsUntyped(NamedQueryTest.java:260)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.hibernate.testing.junit4.ExtendedFrameworkMethod.invokeExplosively(ExtendedFrameworkMethod.java:45)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:299)
at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:293)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.lang.Thread.run(Thread.java:833)
Fixed
Details
Assignee
Marco BelladelliMarco BelladelliReporter
Yoann RodièreYoann RodièreComponents
Sprint
NoneFix versions
Priority
Major
Details
Details
Assignee
Marco Belladelli
Marco BelladelliReporter
Yoann Rodière
Yoann RodièreComponents
Sprint
None
Fix versions
Priority
Created December 14, 2023 at 1:52 PM
Updated January 18, 2024 at 1:38 PM
Resolved December 15, 2023 at 6:29 PM
This code leads to an exception claiming the result class was not specified, even though it was.
final Query query = entityManager.createNativeQuery( "select g.* from Game g where title = ?", Game.class ); entityManagerFactory().addNamedQuery( "the-query", query ); final TypedQuery<Game> namedQuery = entityManager.createNamedQuery( "the-query", Game.class ); namedQuery.setParameter( 1, "Halo" ); assertThat( namedQuery.getResultList() ) .hasSize( 1 ) .element( 0 ) .returns( "Halo", Game::getTitle );
The stacktrace:
java.lang.IllegalArgumentException: Named query exists, but did not specify a resultClass at org.hibernate.query.sql.internal.NativeQueryImpl.<init>(NativeQueryImpl.java:220) at org.hibernate.query.sql.internal.NamedNativeQueryMementoImpl.toQuery(NamedNativeQueryMementoImpl.java:157) at org.hibernate.internal.AbstractSharedSessionContract.createNativeQueryImplementor(AbstractSharedSessionContract.java:1091) at org.hibernate.internal.AbstractSharedSessionContract.lambda$buildNamedQuery$7(AbstractSharedSessionContract.java:1071) at org.hibernate.internal.AbstractSharedSessionContract.buildNamedQuery(AbstractSharedSessionContract.java:1060) at org.hibernate.internal.AbstractSharedSessionContract.buildNamedQuery(AbstractSharedSessionContract.java:1068) at org.hibernate.internal.AbstractSharedSessionContract.createNamedQuery(AbstractSharedSessionContract.java:959) at org.hibernate.internal.SessionImpl.createNamedQuery(SessionImpl.java:200) at org.hibernate.orm.test.jpa.query.NamedQueryTest.lambda$testNamedQueryAddedFromEntityNativeQuery$26(NamedQueryTest.java:236) at org.hibernate.testing.transaction.TransactionUtil.doInJPA(TransactionUtil.java:260) at org.hibernate.testing.transaction.TransactionUtil.doInJPA(TransactionUtil.java:301) at org.hibernate.orm.test.jpa.query.NamedQueryTest.testNamedQueryAddedFromEntityNativeQuery(NamedQueryTest.java:231) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56) at org.hibernate.testing.junit4.ExtendedFrameworkMethod.invokeExplosively(ExtendedFrameworkMethod.java:45) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:299) at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:293) at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) at java.base/java.lang.Thread.run(Thread.java:833)