From 61c66f1d2cc7f29bcbc13e544b0a04e603a07543 Mon Sep 17 00:00:00 2001 From: Henrik Hoffmann Date: Sat, 11 Jan 2014 01:48:01 +0100 Subject: [PATCH] HHH-8850 - fixed ex. when calling addScalar to NamedSQLQuery changed list instanciation from java.util.Arrays.ArrayList to java.util.ArrayList when creating SQLQueryImpl from an NamedSQLQueryDefinition. added testcase --- .../org/hibernate/internal/SQLQueryImpl.java | 4 +-- .../sql/hand/query/NativeSQLQueriesTest.java | 25 ++++++++++++++++++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/internal/SQLQueryImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/SQLQueryImpl.java index abe865ff20..6f5bc091e4 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/SQLQueryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/SQLQueryImpl.java @@ -89,10 +89,10 @@ public class SQLQueryImpl extends AbstractQueryImpl implements SQLQuery { queryDef.getResultSetRef() ); } - this.queryReturns = Arrays.asList( definition.getQueryReturns() ); + this.queryReturns = new ArrayList(Arrays.asList( definition.getQueryReturns() )); } else if ( queryDef.getQueryReturns() != null && queryDef.getQueryReturns().length > 0 ) { - this.queryReturns = Arrays.asList( queryDef.getQueryReturns() ); + this.queryReturns = new ArrayList(Arrays.asList( queryDef.getQueryReturns())); } else { this.queryReturns = new ArrayList(); diff --git a/hibernate-core/src/test/java/org/hibernate/test/sql/hand/query/NativeSQLQueriesTest.java b/hibernate-core/src/test/java/org/hibernate/test/sql/hand/query/NativeSQLQueriesTest.java index d8346297f9..287ed08a18 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/sql/hand/query/NativeSQLQueriesTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/sql/hand/query/NativeSQLQueriesTest.java @@ -9,7 +9,6 @@ import java.util.List; import java.util.Map; import org.junit.Test; - import org.hibernate.Hibernate; import org.hibernate.HibernateException; import org.hibernate.Query; @@ -22,6 +21,8 @@ import org.hibernate.cfg.Environment; import org.hibernate.dialect.H2Dialect; import org.hibernate.dialect.AbstractHANADialect; import org.hibernate.dialect.MySQL5Dialect; +import org.hibernate.engine.query.spi.sql.NativeSQLQueryReturn; +import org.hibernate.engine.spi.NamedSQLQueryDefinitionBuilder; import org.hibernate.internal.util.collections.ArrayHelper; import org.hibernate.test.sql.hand.Dimension; import org.hibernate.test.sql.hand.Employment; @@ -141,6 +142,28 @@ public class NativeSQLQueriesTest extends BaseCoreFunctionalTestCase { s.close(); } } + + @Test + public void testRegisteredNamedSQLQueryWithScalar() + { + final NamedSQLQueryDefinitionBuilder builder = new NamedSQLQueryDefinitionBuilder(); + builder.setName("namedQuery"); + builder.setQuery("select count(*) AS count from organization"); + builder.setQueryReturns(new NativeSQLQueryReturn[1]); + + sessionFactory().registerNamedSQLQueryDefinition("namedQuery", builder.createNamedQueryDefinition()); + + final Session s = openSession(); + s.beginTransaction(); + final SQLQuery query = (SQLQuery) s.getNamedQuery("namedQuery"); + query.addScalar("count"); + final Object result = query.uniqueResult(); + s.getTransaction().commit(); + s.close(); + + assertNotNull(result); + assertEquals(BigInteger.valueOf(0), result); + } @Test public void testManualSynchronization() {