From c53939b181c1235a35693057653c1f93a18a89c8 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Wed, 22 Feb 2017 12:22:25 +0000 Subject: [PATCH] HHH-11510 - NativeQuery#iterate throws QuerySyntaxException instead of UnsupportedOperationException --- .../src/main/java/org/hibernate/Query.java | 2 +- .../query/internal/NativeQueryImpl.java | 6 +++ ...ativeQueryDoesNotSupportIterationTest.java | 46 +++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/sql/NativeQueryDoesNotSupportIterationTest.java diff --git a/hibernate-core/src/main/java/org/hibernate/Query.java b/hibernate-core/src/main/java/org/hibernate/Query.java index 8244ba3173..656c65563f 100644 --- a/hibernate-core/src/main/java/org/hibernate/Query.java +++ b/hibernate-core/src/main/java/org/hibernate/Query.java @@ -369,7 +369,7 @@ public interface Query extends TypedQuery, CommonQueryContract { /** * Return the query results as an Iterator. If the query - * contains multiple results pre row, the results are returned in + * contains multiple results per row, the results are returned in * an instance of Object[].
*
* Entities returned as results are initialized on demand. The first diff --git a/hibernate-core/src/main/java/org/hibernate/query/internal/NativeQueryImpl.java b/hibernate-core/src/main/java/org/hibernate/query/internal/NativeQueryImpl.java index 2139429b47..f4eb4ddfa1 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/internal/NativeQueryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/query/internal/NativeQueryImpl.java @@ -12,6 +12,7 @@ import java.util.Arrays; import java.util.Calendar; import java.util.Collection; import java.util.Date; +import java.util.Iterator; import java.util.List; import java.util.Map; import javax.persistence.FlushModeType; @@ -239,6 +240,11 @@ public class NativeQueryImpl extends AbstractProducedQuery implements Nati } } + @Override + public Iterator iterate() { + throw new UnsupportedOperationException( "SQL queries do not currently support iteration" ); + } + private boolean shouldFlush() { if ( getProducer().isTransactionInProgress() ) { FlushMode effectiveFlushMode = getHibernateFlushMode(); diff --git a/hibernate-core/src/test/java/org/hibernate/test/sql/NativeQueryDoesNotSupportIterationTest.java b/hibernate-core/src/test/java/org/hibernate/test/sql/NativeQueryDoesNotSupportIterationTest.java new file mode 100644 index 0000000000..942209a246 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/sql/NativeQueryDoesNotSupportIterationTest.java @@ -0,0 +1,46 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.sql; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +import org.hibernate.Session; +import org.hibernate.query.NativeQuery; + +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.junit.Test; + +/** + * @author Andrea Boriero + */ +public class NativeQueryDoesNotSupportIterationTest + extends BaseCoreFunctionalTestCase { + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] {TestEntity.class}; + } + + @Test(expected = UnsupportedOperationException.class) + public void iterateShouldThrowAnUnsupportedOperationException() { + try (Session session = openSession();) { + final NativeQuery sqlQuery = session.createNativeQuery( + "select * from TEST_ENTITY" ); + sqlQuery.iterate(); + } + } + + @Entity(name = "TestEntity") + @Table(name = "TEST_ENTITY") + public static class TestEntity { + + @Id + public Long id; + } +}