aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomas Kraus2014-03-07 08:17:46 (EST)
committerGordon Yorke2014-03-10 09:05:33 (EDT)
commit80d1be6f5d3b0ea6da9edbee9381ba6d3827a93c (patch)
tree7a99f3c18142a26c27c4cb9264d5bcd697bda2dd
parenta6e3d9e50815f4de49c3c9549044c686150e0eb2 (diff)
downloadeclipselink.runtime-80d1be6f5d3b0ea6da9edbee9381ba6d3827a93c.zip
eclipselink.runtime-80d1be6f5d3b0ea6da9edbee9381ba6d3827a93c.tar.gz
eclipselink.runtime-80d1be6f5d3b0ea6da9edbee9381ba6d3827a93c.tar.bz2
Bug# 413146 - java.lang.NullPointerException in DatabaseQuery.checkPrepare mapping != null check now covers whole getFieldValue method
Signed-off-by: Tomas Kraus <tomas.kraus@oracle.com>
-rw-r--r--foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/internal/expressions/QueryKeyExpression.java66
-rw-r--r--jpa/eclipselink.jpa.test/src/org/eclipse/persistence/testing/tests/jpa/jpql/JUnitJPQLComplexTestSuite.java36
2 files changed, 69 insertions, 33 deletions
diff --git a/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/internal/expressions/QueryKeyExpression.java b/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/internal/expressions/QueryKeyExpression.java
index f89be89..bd00bfa 100644
--- a/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/internal/expressions/QueryKeyExpression.java
+++ b/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/internal/expressions/QueryKeyExpression.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 1998, 2013 Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2014 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.
@@ -400,42 +400,44 @@ public class QueryKeyExpression extends ObjectExpression {
public Object getFieldValue(Object objectValue, AbstractSession session) {
DatabaseMapping mapping = getMapping();
Object fieldValue = objectValue;
- if ((mapping != null) && (mapping.isAbstractDirectMapping() || mapping.isDirectCollectionMapping())) {
- // CR#3623207, check for IN Collection here not in mapping.
- if (objectValue instanceof Collection) {
- // This can actually be a collection for IN within expressions... however it would be better for expressions to handle this.
- Collection values = (Collection)objectValue;
- Vector fieldValues = new Vector(values.size());
- for (Iterator iterator = values.iterator(); iterator.hasNext();) {
- Object value = iterator.next();
- if (!(value instanceof Expression)){
- value = getFieldValue(value, session);
+ if (mapping != null) {
+ if (mapping.isAbstractDirectMapping() || mapping.isDirectCollectionMapping()) {
+ // CR#3623207, check for IN Collection here not in mapping.
+ if (objectValue instanceof Collection) {
+ // This can actually be a collection for IN within expressions... however it would be better for expressions to handle this.
+ Collection values = (Collection)objectValue;
+ Vector fieldValues = new Vector(values.size());
+ for (Iterator iterator = values.iterator(); iterator.hasNext();) {
+ Object value = iterator.next();
+ if (!(value instanceof Expression)){
+ value = getFieldValue(value, session);
+ }
+ fieldValues.add(value);
+ }
+ fieldValue = fieldValues;
+ } else {
+ if (mapping.isAbstractColumnMapping()) {
+ fieldValue = ((AbstractColumnMapping)mapping).getFieldValue(objectValue, session);
+ } else if (mapping.isDirectCollectionMapping()) {
+ fieldValue = ((DirectCollectionMapping)mapping).getFieldValue(objectValue, session);
}
- fieldValues.add(value);
- }
- fieldValue = fieldValues;
- } else {
- if (mapping.isAbstractColumnMapping()) {
- fieldValue = ((AbstractColumnMapping)mapping).getFieldValue(objectValue, session);
- } else if (mapping.isDirectCollectionMapping()) {
- fieldValue = ((DirectCollectionMapping)mapping).getFieldValue(objectValue, session);
}
- }
- } else if ((objectValue instanceof Collection) && (mapping.isForeignReferenceMapping())) {
- // Was an IN with a collection of objects, extract their ids.
- List ids = new ArrayList();
- for (Object object : ((Collection)objectValue)) {
- if ((mapping.getReferenceDescriptor() != null) && (mapping.getReferenceDescriptor().getJavaClass().isInstance(object))) {
- Object id = mapping.getReferenceDescriptor().getObjectBuilder().extractPrimaryKeyFromObject(object, session);
- if (id instanceof CacheId) {
- id = Arrays.asList(((CacheId)id).getPrimaryKey());
+ } else if ((objectValue instanceof Collection) && (mapping.isForeignReferenceMapping())) {
+ // Was an IN with a collection of objects, extract their ids.
+ List ids = new ArrayList();
+ for (Object object : ((Collection)objectValue)) {
+ if ((mapping.getReferenceDescriptor() != null) && (mapping.getReferenceDescriptor().getJavaClass().isInstance(object))) {
+ Object id = mapping.getReferenceDescriptor().getObjectBuilder().extractPrimaryKeyFromObject(object, session);
+ if (id instanceof CacheId) {
+ id = Arrays.asList(((CacheId)id).getPrimaryKey());
+ }
+ ids.add(id);
+ } else {
+ ids.add(object);
}
- ids.add(id);
- } else {
- ids.add(object);
}
+ fieldValue = ids;
}
- fieldValue = ids;
}
return fieldValue;
diff --git a/jpa/eclipselink.jpa.test/src/org/eclipse/persistence/testing/tests/jpa/jpql/JUnitJPQLComplexTestSuite.java b/jpa/eclipselink.jpa.test/src/org/eclipse/persistence/testing/tests/jpa/jpql/JUnitJPQLComplexTestSuite.java
index f93e20b..4daa78d 100644
--- a/jpa/eclipselink.jpa.test/src/org/eclipse/persistence/testing/tests/jpa/jpql/JUnitJPQLComplexTestSuite.java
+++ b/jpa/eclipselink.jpa.test/src/org/eclipse/persistence/testing/tests/jpa/jpql/JUnitJPQLComplexTestSuite.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 1998, 2013 Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2014 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.
@@ -33,14 +33,22 @@ import org.eclipse.persistence.config.QueryHints;
import org.eclipse.persistence.expressions.Expression;
import org.eclipse.persistence.expressions.ExpressionBuilder;
import org.eclipse.persistence.indirection.IndirectCollection;
+import org.eclipse.persistence.internal.databaseaccess.DatasourceCall;
+import org.eclipse.persistence.internal.expressions.ParameterExpression;
+import org.eclipse.persistence.internal.expressions.QueryKeyExpression;
+import org.eclipse.persistence.internal.jpa.EJBQueryImpl;
+import org.eclipse.persistence.internal.jpa.QueryImpl;
+import org.eclipse.persistence.internal.queries.CallQueryMechanism;
import org.eclipse.persistence.internal.sessions.AbstractSession;
import org.eclipse.persistence.jpa.JpaEntityManager;
import org.eclipse.persistence.logging.SessionLog;
+import org.eclipse.persistence.mappings.DatabaseMapping;
import org.eclipse.persistence.platform.server.oc4j.Oc4jPlatform;
import org.eclipse.persistence.queries.DatabaseQuery;
import org.eclipse.persistence.queries.ReadAllQuery;
import org.eclipse.persistence.queries.ReadObjectQuery;
import org.eclipse.persistence.queries.ReportQuery;
+import org.eclipse.persistence.queries.SQLCall;
import org.eclipse.persistence.sessions.DatabaseSession;
import org.eclipse.persistence.sessions.Session;
import org.eclipse.persistence.testing.framework.QuerySQLTracker;
@@ -236,6 +244,7 @@ public class JUnitJPQLComplexTestSuite extends JUnitTestCase
tests.add("testComplexBetween");
tests.add("testComplexLike");
tests.add("testComplexIn");
+ tests.add("testCollectionQueryKeysFieldValueWithNoMapping");
tests.add("testQueryKeys");
tests.add("complexOneToOneJoinOptimization");
tests.add("testCountOneToManyQueryKey");
@@ -4318,6 +4327,31 @@ public class JUnitJPQLComplexTestSuite extends JUnitTestCase
closeEntityManager(em);
}
+ // Bug# 413146 - Test query with collection as a condition in query for attribute with no mapping.
+ // In current model Employee.startTime has no explicit mapping so QueryKeyExpression.getMapping() returns null.
+ public void testCollectionQueryKeysFieldValueWithNoMapping() {
+ EntityManager em = createEntityManager();
+ // Run query to trigger code path related to Bug# 413146
+ Query query = em.createQuery("Select e.startTime from Employee e where e.startTime in (:time1, :time2)");
+ query.setParameter("time1", new java.util.Date());
+ query.setParameter("time2", new java.util.Date());
+ query.getResultList();
+ // Verify that no mapping exists for Employee.startTime
+ ReportQuery rq = (ReportQuery)((QueryImpl)query).getDatabaseQueryInternal();
+ CallQueryMechanism qm = rq != null ? (CallQueryMechanism)rq.getQueryMechanism() : null;
+ DatasourceCall sc = qm != null ? (DatasourceCall)qm.getDatabaseCall() : null;
+ List params = sc != null ? sc.getParameters() : null;
+ ParameterExpression pe = params != null && params.size() > 0 ? (ParameterExpression)params.get(0) : null;
+ QueryKeyExpression qke = pe != null ? (QueryKeyExpression)pe.getLocalBase() : null;
+ if (qke != null) {
+ DatabaseMapping mapping = qke.getMapping();
+ assertNull("There shall be no mapping for Employee.startTime", mapping);
+ } else {
+ fail("Could not retrieve mapping from QueryKeyExpression.");
+ }
+ closeEntityManager(em);
+ }
+
public void testQueryKeys() {
EntityManager em = createEntityManager();
Query query = em.createQuery("Select e.startTime from Employee e where e.startTime = :time");