fixed bug that caused cached JPQL queries to not properly cache the candidate class.

git-svn-id: https://svn.apache.org/repos/asf/incubator/openjpa/trunk@479942 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Patrick Linskey 2006-11-28 08:27:14 +00:00
parent 8015a253b6
commit 16f78782dd
2 changed files with 86 additions and 6 deletions

View File

@ -52,13 +52,13 @@ import serp.util.Numbers;
/**
* Builder for JPQL expressions. This class takes the query parsed
* in {@link JPQL} and converts it to an expression tree using
* an {@link ExpressionFactory}.
* an {@link ExpressionFactory}. Public for unit testing purposes.
*
* @author Marc Prud'hommeaux
* @author Patrick Linskey
* @nojavadoc
*/
class JPQLExpressionBuilder
public class JPQLExpressionBuilder
extends AbstractExpressionBuilder
implements JPQLTreeConstants {
@ -1616,12 +1616,21 @@ class JPQLExpressionBuilder
}
}
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 @@ class JPQLExpressionBuilder
// 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 ()

View File

@ -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();
}
}