employee example: removed cursor streaming
diff --git a/jpa/employee/employee.model/src/main/java/eclipselink/example/jpa/employee/services/AdminBean.java b/jpa/employee/employee.model/src/main/java/eclipselink/example/jpa/employee/services/AdminBean.java
index 845399c..1ea6da5 100644
--- a/jpa/employee/employee.model/src/main/java/eclipselink/example/jpa/employee/services/AdminBean.java
+++ b/jpa/employee/employee.model/src/main/java/eclipselink/example/jpa/employee/services/AdminBean.java
@@ -15,7 +15,6 @@
import java.util.ArrayList;
import java.util.List;
-import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
diff --git a/jpa/employee/employee.model/src/main/java/eclipselink/example/jpa/employee/services/EmployeeCriteria.java b/jpa/employee/employee.model/src/main/java/eclipselink/example/jpa/employee/services/EmployeeCriteria.java
index b70602c..dd296b9 100644
--- a/jpa/employee/employee.model/src/main/java/eclipselink/example/jpa/employee/services/EmployeeCriteria.java
+++ b/jpa/employee/employee.model/src/main/java/eclipselink/example/jpa/employee/services/EmployeeCriteria.java
@@ -25,7 +25,6 @@
import eclipselink.example.jpa.employee.services.paging.EntityPaging.Type;
import eclipselink.example.jpa.employee.services.paging.FirstMaxPaging;
import eclipselink.example.jpa.employee.services.paging.IdInPaging;
-import eclipselink.example.jpa.employee.services.paging.StreamPaging;
/**
* TODO
@@ -140,8 +139,6 @@
return new FirstMaxPaging(emf, createQuery(emf), createCountQuery(emf), getPageSize());
case PAGE_IN:
return new IdInPaging(emf, createIdQuery(emf), getPageSize());
- case CURSOR:
- return new StreamPaging<Employee>(emf, createQuery(emf), getPageSize());
}
}
return null;
diff --git a/jpa/employee/employee.model/src/main/java/eclipselink/example/jpa/employee/services/EmployeeRepository.java b/jpa/employee/employee.model/src/main/java/eclipselink/example/jpa/employee/services/EmployeeRepository.java
index 4c4d276..75d2146 100644
--- a/jpa/employee/employee.model/src/main/java/eclipselink/example/jpa/employee/services/EmployeeRepository.java
+++ b/jpa/employee/employee.model/src/main/java/eclipselink/example/jpa/employee/services/EmployeeRepository.java
@@ -14,7 +14,6 @@
import java.util.List;
-import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.LockModeType;
diff --git a/jpa/employee/employee.model/src/main/java/eclipselink/example/jpa/employee/services/paging/EntityPaging.java b/jpa/employee/employee.model/src/main/java/eclipselink/example/jpa/employee/services/paging/EntityPaging.java
index 3cd6609..58d7ba2 100644
--- a/jpa/employee/employee.model/src/main/java/eclipselink/example/jpa/employee/services/paging/EntityPaging.java
+++ b/jpa/employee/employee.model/src/main/java/eclipselink/example/jpa/employee/services/paging/EntityPaging.java
@@ -62,7 +62,7 @@
public List<T> next() {
if (!hasNext()) {
- throw new IllegalStateException("Next page available");
+ throw new IllegalStateException("Next page not available");
}
return get(++this.currentPage);
}
@@ -73,10 +73,10 @@
public List<T> previous() {
if (!hasPrevious()) {
- throw new IllegalStateException("Previous page available");
+ throw new IllegalStateException("Previous page not available");
}
return get(--this.currentPage);
}
- public enum Type { NONE, PAGE, PAGE_IN, CURSOR }
+ public enum Type { NONE, PAGE, PAGE_IN }
}
diff --git a/jpa/employee/employee.model/src/main/java/eclipselink/example/jpa/employee/services/paging/StreamPaging.java b/jpa/employee/employee.model/src/main/java/eclipselink/example/jpa/employee/services/paging/StreamPaging.java
deleted file mode 100644
index 06accf9..0000000
--- a/jpa/employee/employee.model/src/main/java/eclipselink/example/jpa/employee/services/paging/StreamPaging.java
+++ /dev/null
@@ -1,103 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 1998, 2013 Oracle. 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
- * which accompanies this distribution.
- * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
- * and the Eclipse Distribution License is available at
- * http://www.eclipse.org/org/documents/edl-v10.php.
- *
- * Contributors:
- * dclarke - initial
- ******************************************************************************/
-package eclipselink.example.jpa.employee.services.paging;
-
-import java.util.List;
-
-import javax.persistence.EntityManager;
-import javax.persistence.EntityManagerFactory;
-import javax.persistence.TypedQuery;
-import javax.persistence.criteria.CriteriaQuery;
-
-import org.eclipse.persistence.config.HintValues;
-import org.eclipse.persistence.config.QueryHints;
-import org.eclipse.persistence.queries.CursoredStream;
-import org.eclipse.persistence.queries.ScrollableCursor;
-
-import eclipselink.example.jpa.employee.model.Employee;
-
-/**
- * Example of using an EclipseLink {@link CursoredStream} to page query results.
- * Generally a stream is used to process query results in memory avoiding the
- * cost of loading all results before starting. This example of using a stream
- * for paging may not be
- *
- * @since EclipseLink 2.4.2
- */
-public class StreamPaging<T> extends EntityPaging<T> {
-
- private ScrollableCursor stream;
-
- public StreamPaging(EntityManagerFactory emf, CriteriaQuery<Employee> criteria, int pageSize) {
- super(null, pageSize);
-
- EntityManager em = emf.createEntityManager();
-
- try {
- TypedQuery<?> query = em.createQuery(criteria);
- query.setHint(QueryHints.SCROLLABLE_CURSOR, HintValues.TRUE);
-
- this.stream = (ScrollableCursor) query.getSingleResult();
- } finally {
- em.close();
- }
- }
-
- @Override
- @SuppressWarnings("unchecked")
- public List<T> next() {
- if (!hasNext()) {
- return super.next();
- }
- int quantity = getPageSize();
- // Adjust size if there are less elements on stream then a full page
- if ((quantity + this.stream.getPosition() - 1) > this.stream.size()) {
- quantity = this.stream.size() - this.stream.getPosition();
- }
- List<T> entities = (List<T>) this.stream.next(quantity);
- this.currentPage++;
- return entities;
- }
-
- @Override
- public List<T> get(int page) {
- if (page == getCurrentPage() + 1) {
- return next();
- }
- int position = (page - 1) * getPageSize();
- this.stream.absolute(position);
- this.currentPage = page - 1;
- return next();
- }
-
- @Override
- public int size() {
- return this.stream.size();
- }
-
- @Override
- public int getNumPages() {
- return (size() / getPageSize()) + (size() % getPageSize() > 0 ? 1 : 0);
- }
-
- /**
- * Release the stream and its corresponding resources.
- */
- public void close() {
- if (this.stream != null) {
- this.stream.close();
- this.stream = null;
- }
- }
-
-}
diff --git a/jpa/employee/employee.model/src/test/java/eclipselink/example/jpa/employee/test/services/EditEmployeeTest.java b/jpa/employee/employee.model/src/test/java/eclipselink/example/jpa/employee/test/services/EditEmployeeTest.java
index 62899bd..4012dd0 100644
--- a/jpa/employee/employee.model/src/test/java/eclipselink/example/jpa/employee/test/services/EditEmployeeTest.java
+++ b/jpa/employee/employee.model/src/test/java/eclipselink/example/jpa/employee/test/services/EditEmployeeTest.java
@@ -15,7 +15,6 @@
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.OptimisticLockException;
-import javax.persistence.RollbackException;
import org.junit.After;
import org.junit.AfterClass;
diff --git a/jpa/employee/employee.model/src/test/java/eclipselink/example/jpa/employee/test/services/StreamEmployeesTest.java b/jpa/employee/employee.model/src/test/java/eclipselink/example/jpa/employee/test/services/StreamEmployeesTest.java
deleted file mode 100644
index 0b36c6e..0000000
--- a/jpa/employee/employee.model/src/test/java/eclipselink/example/jpa/employee/test/services/StreamEmployeesTest.java
+++ /dev/null
@@ -1,346 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2010-2013 Oracle. 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
- * which accompanies this distribution.
- * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
- * and the Eclipse Distribution License is available at
- * http://www.eclipse.org/org/documents/edl-v10.php.
- *
- * Contributors:
- * dclarke - initial
- ******************************************************************************/
-package eclipselink.example.jpa.employee.test.services;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import javax.persistence.EntityManager;
-import javax.persistence.EntityManagerFactory;
-
-import org.junit.After;
-import org.junit.AfterClass;
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.BeforeClass;
-import org.junit.Test;
-
-import eclipselink.example.jpa.employee.model.Employee;
-import eclipselink.example.jpa.employee.model.SamplePopulation;
-import eclipselink.example.jpa.employee.services.Diagnostics;
-import eclipselink.example.jpa.employee.services.Diagnostics.SQLTrace;
-import eclipselink.example.jpa.employee.services.EmployeeCriteria;
-import eclipselink.example.jpa.employee.services.EmployeeRepository;
-import eclipselink.example.jpa.employee.services.paging.EntityPaging;
-import eclipselink.example.jpa.employee.test.PersistenceTesting;
-
-/**
- * TODO
- *
- * @author dclarke
- * @since EclipseLink 2.4.2
- */
-public class StreamEmployeesTest {
-
- @Test
- public void streamAllNext() {
- SQLTrace start = getDiagnostics().start();
- Assert.assertTrue(start.getEntries().isEmpty());
-
- EmployeeCriteria criteria = new EmployeeCriteria();
- criteria.setFirstName(null);
- criteria.setLastName(null);
- criteria.setPageSize(5);
- criteria.setPagingType(EntityPaging.Type.CURSOR.name());
- EntityPaging<Employee> stream = getRepository().getPaging(criteria);
-
- Assert.assertEquals(25, stream.size());
-
- SQLTrace end = getDiagnostics().stop();
-
- Assert.assertNotNull(end);
- Assert.assertSame(start, end);
- Assert.assertFalse(end.getEntries().isEmpty());
- Assert.assertEquals(1, end.getEntries().size());
-
- Assert.assertEquals(5, stream.getNumPages());
-
- // Verify the ids assuming they are sequentially assigned starting at 1.
- int currentId = 1;
-
- for (int index = 0; index < 5; index++) {
- Assert.assertTrue(stream.hasNext());
- List<Employee> emps = stream.next();
-
- Assert.assertEquals(5, emps.size());
-
- for (Employee e : emps) {
- System.out.println("> " + e);
- Assert.assertEquals(currentId++, e.getId());
- }
- }
-
- Assert.assertEquals(1, end.getEntries().size());
- Assert.assertFalse(stream.hasNext());
-
- try {
- stream.next();
- } catch (IllegalStateException e) {
- return;
- }
-
- Assert.fail("IllegalStateException not thrown on next()");
- }
-
- @Test
- public void streamAllNext10() {
- SQLTrace start = getDiagnostics().start();
- Assert.assertTrue(start.getEntries().isEmpty());
-
- EmployeeCriteria criteria = new EmployeeCriteria();
- criteria.setFirstName(null);
- criteria.setLastName(null);
- criteria.setPageSize(10);
- criteria.setPagingType(EntityPaging.Type.CURSOR.name());
- EntityPaging<Employee> stream = getRepository().getPaging(criteria);
-
- Assert.assertEquals(25, stream.size());
-
- SQLTrace end = getDiagnostics().stop();
-
- Assert.assertNotNull(end);
- Assert.assertSame(start, end);
- Assert.assertFalse(end.getEntries().isEmpty());
- Assert.assertEquals(1, end.getEntries().size());
-
- Assert.assertEquals(3, stream.getNumPages());
-
- // Verify the ids assuming they are sequentially assigned starting at 1.
- int currentId = 1;
-
- for (int index = 0; index < 3; index++) {
- Assert.assertTrue(stream.hasNext());
- List<Employee> emps = stream.next();
-
- if (index < 2) {
- Assert.assertEquals(10, emps.size());
- } else {
- Assert.assertEquals(5, emps.size());
- }
-
- for (Employee e : emps) {
- System.out.println("> " + e);
- Assert.assertEquals(currentId++, e.getId());
- }
- }
-
- Assert.assertEquals(1, end.getEntries().size());
- Assert.assertFalse(stream.hasNext());
-
- try {
- stream.next();
- } catch (IllegalStateException e) {
- return;
- }
-
- Assert.fail("IllegalStateException not thrown on next()");
- }
-
- @Test
- public void streamAllPrevious() {
- SQLTrace start = getDiagnostics().start();
- Assert.assertTrue(start.getEntries().isEmpty());
-
- EmployeeCriteria criteria = new EmployeeCriteria();
- criteria.setFirstName(null);
- criteria.setLastName(null);
- criteria.setPageSize(5);
- criteria.setPagingType(EntityPaging.Type.CURSOR.name());
- EntityPaging<Employee> stream = getRepository().getPaging(criteria);
-
- Assert.assertEquals(25, stream.size());
-
- SQLTrace end = getDiagnostics().stop();
-
- Assert.assertNotNull(end);
- Assert.assertSame(start, end);
- Assert.assertFalse(end.getEntries().isEmpty());
- Assert.assertEquals(1, end.getEntries().size());
-
- Assert.assertEquals(5, stream.getNumPages());
-
- // skip to end
- List<List<Employee>> pages = new ArrayList<List<Employee>>();
- pages.add(stream.next());
- Assert.assertEquals(1, stream.getCurrentPage());
- pages.add(stream.next());
- Assert.assertEquals(2, stream.getCurrentPage());
- pages.add(stream.next());
- Assert.assertEquals(3, stream.getCurrentPage());
- pages.add(stream.next());
- Assert.assertEquals(4, stream.getCurrentPage());
- pages.add(stream.next());
- Assert.assertEquals(5, stream.getCurrentPage());
-
- for (int index = 4; index > 0; index--) {
- Assert.assertEquals(index + 1, stream.getCurrentPage());
- Assert.assertTrue("No previous found at page: " + index + " stream at: " + stream.getCurrentPage(), stream.hasPrevious());
-
- List<Employee> emps = stream.previous();
-
- if (index == 1) {
- Assert.assertFalse(stream.hasPrevious());
- } else {
- Assert.assertTrue("No previous found at page: " + index + " stream at: " + stream.getCurrentPage(), stream.hasPrevious());
- }
-
- Assert.assertEquals(5, emps.size());
- Assert.assertEquals(index, stream.getCurrentPage());
-
- List<Employee> nextPage = pages.get(index - 1);
- for (int pi = 0; pi < 5; pi++) {
- Employee emp = emps.get(pi);
- Employee nextEmp = nextPage.get(pi);
- Assert.assertSame(nextEmp, emp);
- System.out.println(index + "> " + emp);
- }
- }
-
- Assert.assertEquals(1, end.getEntries().size());
- Assert.assertTrue(stream.hasNext());
- Assert.assertFalse(stream.hasPrevious());
-
- try {
- stream.previous();
- } catch (IllegalStateException e) {
- return;
- }
-
- Assert.fail("IllegalStateException not thrown on previous()");
- }
-
- @Test
- public void streamAllPreviousGet() {
- SQLTrace start = getDiagnostics().start();
- Assert.assertTrue(start.getEntries().isEmpty());
-
- EmployeeCriteria criteria = new EmployeeCriteria();
- criteria.setFirstName(null);
- criteria.setLastName(null);
- criteria.setPageSize(5);
- criteria.setPagingType(EntityPaging.Type.CURSOR.name());
-
- EntityPaging<Employee> stream = getRepository().getPaging(criteria);
-
- Assert.assertEquals(25, stream.size());
-
- SQLTrace end = getDiagnostics().stop();
-
- Assert.assertNotNull(end);
- Assert.assertSame(start, end);
- Assert.assertFalse(end.getEntries().isEmpty());
- Assert.assertEquals(1, end.getEntries().size());
-
- Assert.assertEquals(5, stream.getNumPages());
-
- // skip to end
- List<List<Employee>> pages = new ArrayList<List<Employee>>();
- pages.add(stream.next());
- Assert.assertEquals(1, stream.getCurrentPage());
- pages.add(stream.next());
- Assert.assertEquals(2, stream.getCurrentPage());
- pages.add(stream.next());
- Assert.assertEquals(3, stream.getCurrentPage());
- pages.add(stream.next());
- Assert.assertEquals(4, stream.getCurrentPage());
- pages.add(stream.next());
- Assert.assertEquals(5, stream.getCurrentPage());
-
- for (int index = 4; index > 0; index--) {
- Assert.assertEquals(index + 1, stream.getCurrentPage());
- Assert.assertTrue("No previous found at page: " + index + " stream at: " + stream.getCurrentPage(), stream.hasPrevious());
-
- List<Employee> emps = stream.get(index);
-
- if (index == 1) {
- Assert.assertFalse(stream.hasPrevious());
- } else {
- Assert.assertTrue("No previous found at page: " + index + " stream at: " + stream.getCurrentPage(), stream.hasPrevious());
- }
-
- Assert.assertEquals(5, emps.size());
- Assert.assertEquals(index, stream.getCurrentPage());
-
- List<Employee> nextPage = pages.get(index - 1);
- for (int pi = 0; pi < 5; pi++) {
- Employee emp = emps.get(pi);
- Employee nextEmp = nextPage.get(pi);
- Assert.assertSame(nextEmp, emp);
- System.out.println(index + "> " + emp);
- }
- }
-
- Assert.assertEquals(1, end.getEntries().size());
- Assert.assertTrue(stream.hasNext());
- Assert.assertFalse(stream.hasPrevious());
-
- try {
- stream.previous();
- } catch (IllegalStateException e) {
- return;
- }
-
- Assert.fail("IllegalStateException not thrown on previous()");
- }
-
- private static EntityManagerFactory emf;
-
- public static EntityManagerFactory getEmf() {
- return emf;
- }
-
- @BeforeClass
- public static void createEMF() {
- emf = PersistenceTesting.createEMF(true);
-
- EntityManager em = emf.createEntityManager();
- em.getTransaction().begin();
- new SamplePopulation().createNewEmployees(em, 25);
- em.getTransaction().commit();
- em.close();
-
- emf.getCache().evictAll();
- }
-
- @AfterClass
- public static void closeEMF() {
- if (emf != null && emf.isOpen()) {
- emf.close();
- }
- emf = null;
- }
-
- private EmployeeRepository repository;
-
- @Before
- public void setup() {
- this.repository = new EmployeeRepository();
- this.repository.setEntityManager(getEmf().createEntityManager());
- this.repository.getEntityManager().getTransaction().begin();
- }
-
- @After
- public void close() {
- this.repository.getEntityManager().getTransaction().commit();
- this.repository.getEntityManager().close();
- }
-
- public EmployeeRepository getRepository() {
- return repository;
- }
-
- public Diagnostics getDiagnostics() {
- return getRepository().getDiagnostics();
- }
-
-}
diff --git a/jpa/employee/employee.web/src/main/webapp/admin.xhtml b/jpa/employee/employee.web/src/main/webapp/admin.xhtml
index d4b226a..3ab4e3c 100644
--- a/jpa/employee/employee.web/src/main/webapp/admin.xhtml
+++ b/jpa/employee/employee.web/src/main/webapp/admin.xhtml
@@ -21,10 +21,12 @@
action="#{admin.populateDatabase}" class="menu-button"
style="width: 400" />
<p />
+ <!--
<h:commandButton value="#{admin.toggleSqlDisplayButton}"
action="#{admin.toggleSqlDisplay}" class="menu-button"
style="width: 400" />
<p />
+ -->
</div>
</h:form>
<h2>Entity Status</h2>
diff --git a/jpa/employee/employee.web/src/main/webapp/employee/search.xhtml b/jpa/employee/employee.web/src/main/webapp/employee/search.xhtml
index 9f87a2d..9f78340 100644
--- a/jpa/employee/employee.web/src/main/webapp/employee/search.xhtml
+++ b/jpa/employee/employee.web/src/main/webapp/employee/search.xhtml
@@ -25,8 +25,6 @@
<f:selectItem itemValue="NONE" itemLabel="None" />
<f:selectItem itemValue="PAGE" itemLabel="First/Max Paging" />
<f:selectItem itemValue="PAGE_IN" itemLabel="Ids IN Paging" />
- <f:selectItem itemValue="CURSOR"
- itemLabel="ScrollableCursor Paging" />
</h:selectOneMenu>
<f:facet name="footer">
<div align="right">