[jpa-spec-99] Add ability to stream the result of a query execution
see: https://github.com/javaee/jpa-spec/issues/99
Signed-off-by: Lukas Jungmann <lukas.jungmann@oracle.com>
diff --git a/src/javax/persistence/Query.java b/src/javax/persistence/Query.java
index 0bd870f..2a86679 100644
--- a/src/javax/persistence/Query.java
+++ b/src/javax/persistence/Query.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2008 - 2013 Oracle Corporation. All rights reserved.
+ * Copyright (c) 2008 - 2017 Oracle Corporation. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
@@ -9,6 +9,7 @@
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
+ * Lukas Jungmann - Java Persistence 2.2
* Linda DeMichiel - Java Persistence 2.1
* Linda DeMichiel - Java Persistence 2.0
*
@@ -20,6 +21,7 @@
import java.util.List;
import java.util.Set;
import java.util.Map;
+import java.util.stream.Stream;
/**
* Interface used to control query execution.
@@ -55,6 +57,37 @@
List getResultList();
/**
+ * Execute a SELECT query and return the query results
+ * as an untyped <code>java.util.stream.Stream</code>.
+ * By default this method delegates to <code>getResultList().stream()</code>,
+ * however persistence provider may choose to override this method
+ * to provide additional capabilities.
+ *
+ * @return a stream of the results
+ * @throws IllegalStateException if called for a Java
+ * Persistence query language UPDATE or DELETE statement
+ * @throws QueryTimeoutException if the query execution exceeds
+ * the query timeout value set and only the statement is
+ * rolled back
+ * @throws TransactionRequiredException if a lock mode other than
+ * <code>NONE</code> has been set and there is no transaction
+ * or the persistence context has not been joined to the transaction
+ * @throws PessimisticLockException if pessimistic locking
+ * fails and the transaction is rolled back
+ * @throws LockTimeoutException if pessimistic locking
+ * fails and only the statement is rolled back
+ * @throws PersistenceException if the query execution exceeds
+ * the query timeout value set and the transaction
+ * is rolled back
+ * @see Stream
+ * @see #getResultList()
+ * @since 2.2
+ */
+ default Stream getResultStream() {
+ return getResultList().stream();
+ }
+
+ /**
* Execute a SELECT query that returns a single untyped result.
* @return the result
* @throws NoResultException if there is no result
diff --git a/src/javax/persistence/TypedQuery.java b/src/javax/persistence/TypedQuery.java
index 3a42b49..87746bf 100644
--- a/src/javax/persistence/TypedQuery.java
+++ b/src/javax/persistence/TypedQuery.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2008 - 2013 Oracle Corporation. All rights reserved.
+ * Copyright (c) 2008 - 2017 Oracle Corporation. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
@@ -9,6 +9,7 @@
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
+ * Lukas Jungmann - Java Persistence 2.2
* Linda DeMichiel - Java Persistence 2.1
* Linda DeMichiel - Java Persistence 2.0
*
@@ -18,6 +19,7 @@
import java.util.List;
import java.util.Date;
import java.util.Calendar;
+import java.util.stream.Stream;
/**
* Interface used to control the execution of typed queries.
@@ -54,6 +56,37 @@
List<X> getResultList();
/**
+ * Execute a SELECT query and return the query results
+ * as a typed <code>java.util.stream.Stream</code>.
+ * By default this method delegates to <code>getResultList().stream()</code>,
+ * however persistence provider may choose to override this method
+ * to provide additional capabilities.
+ *
+ * @return a stream of the results
+ * @throws IllegalStateException if called for a Java
+ * Persistence query language UPDATE or DELETE statement
+ * @throws QueryTimeoutException if the query execution exceeds
+ * the query timeout value set and only the statement is
+ * rolled back
+ * @throws TransactionRequiredException if a lock mode other than
+ * <code>NONE</code> has been set and there is no transaction
+ * or the persistence context has not been joined to the transaction
+ * @throws PessimisticLockException if pessimistic locking
+ * fails and the transaction is rolled back
+ * @throws LockTimeoutException if pessimistic locking
+ * fails and only the statement is rolled back
+ * @throws PersistenceException if the query execution exceeds
+ * the query timeout value set and the transaction
+ * is rolled back
+ * @see Stream
+ * @see #getResultList()
+ * @since 2.2
+ */
+ default Stream<X> getResultStream() {
+ return getResultList().stream();
+ }
+
+ /**
* Execute a SELECT query that returns a single result.
* @return the result
* @throws NoResultException if there is no result