From b4077bd6a64f73fd28c210f265d5ef8b089199dd Mon Sep 17 00:00:00 2001 From: Marc Prud'hommeaux Date: Fri, 3 Nov 2006 21:10:39 +0000 Subject: [PATCH] OPENJPA-74: Fixed multiple problems with named queries: 1. They were not being parsed as part of the orm.xml file, since we were incorrectly looking for an attribute named 'query' (when it really should have been a sub-element named 'query'). 2. Looking for a named query did not force resolution of all metadatas. 3. An NPE was being thrown when a JPQL query was created with a null filter git-svn-id: https://svn.apache.org/repos/asf/incubator/openjpa/trunk@470996 13f79535-47bb-0310-9956-ffa450edef68 --- .../kernel/jpql/JPQLExpressionBuilder.java | 3 ++ .../openjpa/meta/MetaDataRepository.java | 31 +++++++++++++++++-- .../apache/openjpa/meta/localizer.properties | 4 ++- .../openjpa/persistence/MetaDataTag.java | 1 + .../XMLPersistenceMetaDataParser.java | 24 ++++++++++++++ 5 files changed, 60 insertions(+), 3 deletions(-) diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/jpql/JPQLExpressionBuilder.java b/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/jpql/JPQLExpressionBuilder.java index a8804ea97..8b2097e5e 100644 --- a/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/jpql/JPQLExpressionBuilder.java +++ b/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/jpql/JPQLExpressionBuilder.java @@ -1628,6 +1628,9 @@ class JPQLExpressionBuilder } private static final JPQLNode parse(String jpql) { + if (jpql == null) + jpql = ""; + try { return (JPQLNode) new JPQL(jpql).parseQuery(); } catch (Error e) { diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java b/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java index bb54de558..48cdc71f7 100644 --- a/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java +++ b/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java @@ -1461,11 +1461,38 @@ public class MetaDataRepository public synchronized QueryMetaData getQueryMetaData(Class cls, String name, ClassLoader envLoader, boolean mustExist) { QueryMetaData meta = getQueryMetaDataInternal(cls, name, envLoader); - if (meta == null && mustExist) - throw new MetaDataException(_loc.get("no-named-query", cls, name)); + if (meta == null) { + // load all the metadatas for all the known classes so that + // query names are seen and registered + resolveAll(envLoader); + meta = getQueryMetaDataInternal(cls, name, envLoader); + } + + if (meta == null && mustExist) { + if (cls == null) { + throw new MetaDataException(_loc.get + ("no-named-query-null-class", + getPersistentTypeNames(false, envLoader), name)); + } else { + throw new MetaDataException(_loc.get("no-named-query", + cls, name)); + } + } + return meta; } + /** + * Resolve all known metadata classes. + */ + private void resolveAll(ClassLoader envLoader) { + Collection types = loadPersistentTypes(false, envLoader); + for (Iterator i = types.iterator(); i.hasNext(); ) { + Class c = (Class) i.next(); + getMetaData(c, envLoader, false); + } + } + /** * Return query metadata for the given class, name, and classloader. */ diff --git a/openjpa-kernel/src/main/resources/org/apache/openjpa/meta/localizer.properties b/openjpa-kernel/src/main/resources/org/apache/openjpa/meta/localizer.properties index 31e2c9534..601f70066 100644 --- a/openjpa-kernel/src/main/resources/org/apache/openjpa/meta/localizer.properties +++ b/openjpa-kernel/src/main/resources/org/apache/openjpa/meta/localizer.properties @@ -204,6 +204,8 @@ tool-usage: Usage: java org.apache.openjpa.meta.MetaDataTool\n\ \t+ no-named-query: There is no named query defined for the class "{0}" with \ the name "{1}". +no-named-query-null-class: There is no query with the name "{1}" defined for \ + any of the known persistent classes: {0}. no-named-sequence: Could not locate the sequence with name "{0}". It is \ possible that the metadata file containing the sequence has not been \ parsed yet. Make sure to use a persistent class whose metadata is in \ @@ -274,4 +276,4 @@ no-metadatafactory: MetaDataFactory could not be configured \ using Ant, please see the or attributes \ of the task''s nested element. This can also occur if your \ OpenJPA distribution jars are corrupt, or if your security policy is \ - overly strict. \ No newline at end of file + overly strict. diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/MetaDataTag.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/MetaDataTag.java index 47d35a047..2d9132688 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/MetaDataTag.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/MetaDataTag.java @@ -42,6 +42,7 @@ public enum MetaDataTag { MAP_KEY, NATIVE_QUERIES, NATIVE_QUERY, + QUERY_STRING, ORDER_BY, QUERIES, QUERY, diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/XMLPersistenceMetaDataParser.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/XMLPersistenceMetaDataParser.java index 01ca9be5a..c76eaeb18 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/XMLPersistenceMetaDataParser.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/XMLPersistenceMetaDataParser.java @@ -119,6 +119,7 @@ public class XMLPersistenceMetaDataParser _elems.put("named-query", QUERY); _elems.put("named-native-query", NATIVE_QUERY); _elems.put("query-hint", QUERY_HINT); + _elems.put("query", QUERY_STRING); _elems.put("flush-mode", FLUSH_MODE); _elems.put("sequence-generator", SEQ_GENERATOR); @@ -442,6 +443,9 @@ public class XMLPersistenceMetaDataParser case NATIVE_QUERY: ret = startNamedNativeQuery(attrs); break; + case QUERY_STRING: + ret = startQueryString(attrs); + break; case SEQ_GENERATOR: ret = startSequenceGenerator(attrs); break; @@ -489,6 +493,9 @@ public class XMLPersistenceMetaDataParser case NATIVE_QUERY: endNamedNativeQuery(); break; + case QUERY_STRING: + endQueryString(); + break; case SEQ_GENERATOR: endSequenceGenerator(); break; @@ -553,6 +560,9 @@ public class XMLPersistenceMetaDataParser case NATIVE_QUERY: ret = startNamedNativeQuery(attrs); break; + case QUERY_STRING: + ret = startQueryString(attrs); + break; case SEQ_GENERATOR: ret = startSequenceGenerator(attrs); break; @@ -640,6 +650,9 @@ public class XMLPersistenceMetaDataParser case NATIVE_QUERY: endNamedNativeQuery(); break; + case QUERY_STRING: + endQueryString(); + break; case SEQ_GENERATOR: endSequenceGenerator(); break; @@ -1368,6 +1381,17 @@ public class XMLPersistenceMetaDataParser popElement(); } + protected boolean startQueryString(Attributes attrs) + throws SAXException { + return true; + } + + protected void endQueryString() + throws SAXException { + QueryMetaData meta = (QueryMetaData) currentElement(); + meta.setQueryString(currentText()); + } + /** * Parse query-hint. */