Bug #453208 - Pessimistic locking with query row limits does not work with Oracle database - minor fix

Signed-off-by: Petros Splinakis <petros.splinakis@oracle.com>
Reviewed-by: Lukas Jungmann <lukas.jungmann@oracle.com>
diff --git a/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/platform/database/OraclePlatform.java b/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/platform/database/OraclePlatform.java
index d9430d9..c4d4df8 100644
--- a/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/platform/database/OraclePlatform.java
+++ b/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/platform/database/OraclePlatform.java
@@ -32,6 +32,7 @@
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Types;
+import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.Hashtable;
 import java.util.Iterator;
@@ -975,6 +976,7 @@ public void printSQLSelectStatement(DatabaseCall call, ExpressionSQLPrinter prin
                     Vector fields = new Vector();
                     statement.enableFieldAliasesCaching();
                     String queryString = printOmittingForUpdate(statement, printer, fields);
+                    duplicateCallParameters(call);
                     call.setFields(fields);
 
                     /* Prints a query similar to the below:
@@ -1040,6 +1042,17 @@ public void printSQLSelectStatement(DatabaseCall call, ExpressionSQLPrinter prin
     }
 
     @SuppressWarnings("unchecked")
+    // Bug #453208 - Duplicate call parameters since the query is performed twice
+    private void duplicateCallParameters(DatabaseCall call) {
+        ArrayList newParameterList = new ArrayList(call.getParameters());
+        newParameterList.addAll(call.getParameters());
+        call.setParameters(newParameterList);
+        ArrayList<Integer> newParameterTypesList = new ArrayList(call.getParameterTypes());
+        newParameterTypesList.addAll(call.getParameterTypes());
+        call.setParameterTypes(newParameterTypesList);
+    }
+
+    @SuppressWarnings("unchecked")
     private String printOmittingForUpdate(SQLSelectStatement statement, ExpressionSQLPrinter printer, Vector fields) {
         boolean originalShouldPrintForUpdate = this.shouldPrintForUpdateClause;
         Writer originalWriter = printer.getWriter();
diff --git a/jpa/eclipselink.jpa.test/src/org/eclipse/persistence/testing/tests/jpa/jpql/AdvancedQueryTestSuite.java b/jpa/eclipselink.jpa.test/src/org/eclipse/persistence/testing/tests/jpa/jpql/AdvancedQueryTestSuite.java
index 804a30b..5379fd8 100644
--- a/jpa/eclipselink.jpa.test/src/org/eclipse/persistence/testing/tests/jpa/jpql/AdvancedQueryTestSuite.java
+++ b/jpa/eclipselink.jpa.test/src/org/eclipse/persistence/testing/tests/jpa/jpql/AdvancedQueryTestSuite.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 1998, 2015 Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2016 Oracle and/or its affiliates. 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.
@@ -29,6 +29,7 @@
 import javax.persistence.Query;
 import javax.persistence.EntityManager;
 import javax.persistence.RollbackException;
+import javax.persistence.TypedQuery;
 
 import junit.framework.Test;
 import junit.framework.TestSuite;
@@ -2714,7 +2715,8 @@ public void testQueryPESSIMISTICLockWithLimit() throws InterruptedException {
         EntityManager em = createEntityManager();
         beginTransaction(em);
         try {
-            Query query = em.createQuery("Select e from Employee e");
+            TypedQuery<Employee> query = em.createQuery("Select e from Employee e where e.lastName != :lastName", Employee.class);
+            query.setParameter("lastName", "Chanley");
             query.setHint(QueryHints.PESSIMISTIC_LOCK, PessimisticLock.Lock);
             query.setFirstResult(5);
             query.setMaxResults(2);
@@ -2734,14 +2736,14 @@ public void testQueryPESSIMISTICLockWithLimit() throws InterruptedException {
                         public void run() {
                         try {
                             beginTransaction(em2);
-                            Query query2 = em2.createQuery("select e from Employee e where e.id = :id");
+                            TypedQuery<Employee> query2 = em2.createQuery("select e from Employee e where e.id = :id", Employee.class);
                             query2.setParameter("id", e.getId());
                             query2.setHint(QueryHints.PESSIMISTIC_LOCK_TIMEOUT, 5);
-                            Employee emp = (Employee) query2.getSingleResult(); // might wait for lock to be released
+                            Employee emp = query2.getSingleResult(); // might wait for lock to be released
                             emp.setFirstName("Trouba");
                             commitTransaction(em2); // might wait for lock to be released
                         } catch (javax.persistence.RollbackException ex) {
-                            if (ex.getMessage().indexOf("org.eclipse.persistence.exceptions.DatabaseException") == -1) {
+                            if (!ex.getMessage().contains("org.eclipse.persistence.exceptions.DatabaseException")) {
                                 ex.printStackTrace();
                                 fail("it's not the right exception:" + ex);
                             }