Author: pcl
Date: Tue Nov 28 00:27:14 2006
New Revision: 479942
URL: http://svn.apache.org/viewvc?view=rev&rev=479942
Log:
fixed bug that caused cached JPQL queries to not properly cache the candidate
class.
Added:
incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/kernel/
incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/kernel/TestQueryCompilationCache.java
(with props)
Modified:
incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/jpql/JPQLExpressionBuilder.java
Modified:
incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/jpql/JPQLExpressionBuilder.java
URL:
http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/jpql/JPQLExpressionBuilder.java?view=diff&rev=479942&r1=479941&r2=479942
==============================================================================
---
incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/jpql/JPQLExpressionBuilder.java
(original)
+++
incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/jpql/JPQLExpressionBuilder.java
Tue Nov 28 00:27:14 2006
@@ -52,13 +52,13 @@
/**
* Builder for JPQL expressions. This class takes the query parsed
* in [EMAIL PROTECTED] JPQL} and converts it to an expression tree using
- * an [EMAIL PROTECTED] ExpressionFactory}.
+ * an [EMAIL PROTECTED] ExpressionFactory}. Public for unit testing purposes.
*
* @author Marc Prud'hommeaux
* @author Patrick Linskey
* @nojavadoc
*/
-class JPQLExpressionBuilder
+public class JPQLExpressionBuilder
extends AbstractExpressionBuilder
implements JPQLTreeConstants {
@@ -1616,11 +1616,20 @@
}
}
- static class ParsedJPQL
+ /**
+ * Public for unit testing purposes.
+ * @nojavadoc
+ */
+ public static class ParsedJPQL
implements Serializable {
protected final JPQLNode root;
protected final String query;
+
+ // cache of candidate type data. This is stored here in case this
+ // parse tree is reused in a context that does not know what the
+ // candidate type is already.
+ private Class _candidateType;
ParsedJPQL(String jpql) {
this(jpql, parse(jpql));
@@ -1653,9 +1662,19 @@
// if the owning query's context does not have
// any candidate class, then set it here
- if (ctx.getCandidateType() == null)
- ctx.setCandidateType(new JPQLExpressionBuilder
- (null, query, this).getCandidateType(), true);
+ if (ctx.getCandidateType() == null) {
+ if (_candidateType == null)
+ _candidateType = new JPQLExpressionBuilder
+ (null, query, this).getCandidateType();
+ ctx.setCandidateType(_candidateType, true);
+ }
+ }
+
+ /**
+ * Public for unit testing purposes.
+ */
+ public Class getCandidateType() {
+ return _candidateType;
}
public String toString ()
Added:
incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/kernel/TestQueryCompilationCache.java
URL:
http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/kernel/TestQueryCompilationCache.java?view=auto&rev=479942
==============================================================================
---
incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/kernel/TestQueryCompilationCache.java
(added)
+++
incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/kernel/TestQueryCompilationCache.java
Tue Nov 28 00:27:14 2006
@@ -0,0 +1,61 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openjpa.kernel;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.persistence.Persistence;
+
+import org.apache.openjpa.kernel.QueryImpl.Compilation;
+import org.apache.openjpa.kernel.jpql.JPQLExpressionBuilder.ParsedJPQL;
+import org.apache.openjpa.persistence.OpenJPAEntityManager;
+import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
+import org.apache.openjpa.persistence.OpenJPAPersistence;
+import org.apache.openjpa.persistence.OpenJPAQuery;
+import org.apache.openjpa.persistence.query.NamedEntity;
+
+import junit.framework.TestCase;
+
+
+public class TestQueryCompilationCache
+ extends TestCase {
+
+ public void testDynamicJPQLWithNamedEntity() {
+ Map props = new HashMap();
+ props.put("openjpa.MetaDataFactory", "jpa(Types="
+ + NamedEntity.class.getName() + ")");
+ OpenJPAEntityManagerFactory emf = OpenJPAPersistence.cast(
+ Persistence.createEntityManagerFactory("test", props));
+
+ Map cache = emf.getConfiguration().getQueryCompilationCacheInstance();
+ cache.clear();
+ OpenJPAEntityManager em = emf.createEntityManager();
+ OpenJPAQuery q = em.createQuery("select o from named o");
+ q.compile();
+ em.close();
+
+ // make sure that there's an entry in the cache now
+ assertEquals(1, cache.size());
+
+ // dig into the entry and check its internal state
+ Compilation comp = (Compilation) cache.values().iterator().next();
+ assertEquals(NamedEntity.class,
+ ((ParsedJPQL) comp.storeData).getCandidateType());
+
+ emf.close();
+ }
+}
Propchange:
incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/kernel/TestQueryCompilationCache.java
------------------------------------------------------------------------------
svn:executable = *