When creating an Query via NamedSQLQueryDefinition (eg. via @NamedNativeQuery annotation in JavaEE) an java.lang.UnsupportedOperationException will be thrown when invoking addScalar(..)
This could be verified by exact code review:
For me, the fault seems to be at the constructor org.hibernate.intenal.SQLQueryImpl(NamedSQLQueryDefinition queryDef, SessionImplementor session, ParameterMetadata parameterMetadata).
At line 95 the local variable 'queryReturns' is initialized by using Arrays.asList(..). This will create an simplified derivation of AbstractList wich will only provide reading operations but when executing the query after adding an scalar SqlQueryImpl.prepare() will call queryReturns.clear() (see line 233).
The other constructor org.hibernate.internal.SQLQueryImpl(final String sql, final String returnAliases, final Class returnClasses, final SessionImplementor session, ParameterMetadata parameterMetadata) is doing this better.
As you can see at line 140 the local variable 'queryReturns' is initalized by using new ArrayList<NativeSQLQueryReturn>() instead of Arrays.asList(..).
In Hibernate 3 the Bug was allready existent, but the exception has been thrown when calling addScalar(), now it just will be thrown when excecute the query.
JBoss EAP 6.1.1, Oracle10g, Hibernate 4.2.0.SP1
"Real" JEE Example for JBoss EAP 6.1.x, showing the mentioned UnsupportedOperationException.
Its based on hibernate4 quickstart project.
oh well, for an "real" JEE application showing this exception watch ExceptionWithNamedQueryAndAddScalar.zip
for an synthetic testcase please have a look on my pull request
Thank you for the pull request!
Could you please provide this bugfix for 4.2.9?
Sorry , it was already in the 4.2 branch, but I just didn't set the fix version for some reason. Thanks for pinging me!