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
This commit is contained in:
Marc Prud'hommeaux 2006-11-03 21:10:39 +00:00
parent 22226fc0f4
commit b4077bd6a6
5 changed files with 60 additions and 3 deletions

View File

@ -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) {

View File

@ -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.
*/

View File

@ -204,6 +204,8 @@ tool-usage: Usage: java org.apache.openjpa.meta.MetaDataTool\n\
\t<class name | .java file | .class file>+
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 <properties> or <propertiesFile> attributes \
of the task''s nested <config> element. This can also occur if your \
OpenJPA distribution jars are corrupt, or if your security policy is \
overly strict.
overly strict.

View File

@ -42,6 +42,7 @@ public enum MetaDataTag {
MAP_KEY,
NATIVE_QUERIES,
NATIVE_QUERY,
QUERY_STRING,
ORDER_BY,
QUERIES,
QUERY,

View File

@ -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.
*/