OPENJPA-111: Modified tests to use correct binding parameter. Added methods in MetaDataRepository to look for NamedQuery only by name without defining class information.

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@686419 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Pinaki Poddar 2008-08-15 23:36:16 +00:00
parent a7b9f8f7f3
commit d9712537da
8 changed files with 58 additions and 25 deletions

View File

@ -1618,10 +1618,15 @@ public class MetaDataRepository
ClassLoader envLoader) {
if (name == null)
return null;
QueryMetaData qm = null;
if (cls == null) {
qm = searchQueryMetaDataByName(name);
if (qm != null)
return qm;
}
// check cache
Object key = getQueryKey(cls, name);
QueryMetaData qm = (QueryMetaData) _queries.get(key);
qm = (QueryMetaData) _queries.get(key);
if (qm != null)
return qm;
@ -1694,6 +1699,18 @@ public class MetaDataRepository
return false;
return _queries.remove(getQueryKey(cls, name)) != null;
}
/**
* Searches all cached query metadata by name.
*/
public QueryMetaData searchQueryMetaDataByName(String name) {
for (Object key : _queries.keySet()) {
if (key instanceof QueryKey)
if (StringUtils.equals(((QueryKey)key).name, name))
return (QueryMetaData)_queries.get(key);
}
return null;
}
/**
* Return a unique key for a given QueryMetaData.

View File

@ -33,11 +33,11 @@ import javax.persistence.NamedQuery;
import javax.persistence.SqlResultSetMapping;
import javax.persistence.Table;
@NamedQuery(name="FindXTwo", query="select s from simple s where s.name = :fname")
@NamedQuery(name="FindXTwo", query="select s from simple s where s.name = ?1")
@NamedQueries( {
@NamedQuery(name="FindOne", query="select s from simple s where s.name = :fname"),
@NamedQuery(name="FindOne", query="select s from simple s where s.name = :fname"),
@NamedQuery(name="FindOne", query="select s from simple s where s.name = ?1"),
@NamedQuery(name="FindOne", query="select s from simple s where s.name = ?1"),
@NamedQuery(name="FindAll", query="select s from simple s")
})

View File

@ -27,11 +27,11 @@ import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
@NamedQuery(name="FindXTwo", query="select s from simple2 s where s.name = :fname")
@NamedQuery(name="FindXTwo", query="select s from simple2 s where s.name = ?1")
@NamedQueries( {
@NamedQuery(name="FindOne", query="select s from simple2 s where s.name = :fname"),
@NamedQuery(name="Find2One", query="select s from simple2 s where s.name = :fname"),
@NamedQuery(name="FindOne", query="select s from simple2 s where s.name = ?1"),
@NamedQuery(name="Find2One", query="select s from simple2 s where s.name = ?1"),
@NamedQuery(name="Find2All", query="select s from simple2 s")
})

View File

@ -55,13 +55,13 @@ public class TestDupNamedQuery extends SingleEMFTestCase {
String ValueTwo) {
EntityManager em = emf.createEntityManager();
List list = em.createNamedQuery(findOneQName).setParameter("fname", nameOne)
List list = em.createNamedQuery(findOneQName).setParameter(1, nameOne)
.getResultList();
assertNotNull(list);
assertEquals(list.size(), 1);
Object o = list.get(0);
assertSame(o.getClass(), simple2 ? SimpleEntity2.class
: SimpleEntity.class);
assertTrue(simple2 ? o instanceof SimpleEntity2
: o instanceof SimpleEntity);
assertEquals(simple2 ? ((SimpleEntity2) o).getValue()
: ((SimpleEntity) o).getValue(), ValueOne);
@ -71,8 +71,8 @@ public class TestDupNamedQuery extends SingleEMFTestCase {
assertEquals(list.size(), 2);
for (Iterator resultIter = list.iterator(); resultIter.hasNext();) {
o = resultIter.next();
assertSame(o.getClass(), simple2 ? SimpleEntity2.class
: SimpleEntity.class);
assertTrue(simple2 ? o instanceof SimpleEntity2
: o instanceof SimpleEntity);
String n = null;
String v = null;
if (simple2) {

View File

@ -1582,14 +1582,17 @@ public class AnnotationPersistenceMetaDataParser
if (_log.isTraceEnabled())
_log.trace(_loc.get("parse-query", query.name()));
meta = getRepository().getCachedQueryMetaData(null, query.name());
meta = getRepository().searchQueryMetaDataByName(query.name());
if (meta != null) {
if (_log.isWarnEnabled())
_log.warn(_loc.get("dup-query", query.name(), el));
Class definingType = meta.getDefiningType();
if ((definingType == null || definingType != _cls)
&& _log.isWarnEnabled()) {
_log.warn(_loc.get("dup-query", query.name(), el,
definingType));
}
continue;
}
meta = getRepository().addQueryMetaData(null, query.name());
meta = getRepository().addQueryMetaData(_cls, query.name());
meta.setQueryString(query.query());
meta.setLanguage(JPQLParser.LANG_JPQL);
for (QueryHint hint : query.hints())
@ -1623,10 +1626,12 @@ public class AnnotationPersistenceMetaDataParser
if (_log.isTraceEnabled())
_log.trace(_loc.get("parse-native-query", query.name()));
meta = getRepository().getCachedQueryMetaData(null, query.name());
meta = getRepository().searchQueryMetaDataByName(query.name());
if (meta != null) {
if (_log.isWarnEnabled())
_log.warn(_loc.get("dup-query", query.name(), el));
Class defType = meta.getDefiningType();
if ((defType != _cls) && _log.isWarnEnabled()) {
_log.warn(_loc.get("dup-query", query.name(), el, defType));
}
continue;
}

View File

@ -1407,9 +1407,15 @@ public class XMLPersistenceMetaDataParser
if (log.isTraceEnabled())
log.trace(_loc.get("parse-query", name));
QueryMetaData meta = getRepository().getCachedQueryMetaData(null, name);
if (meta != null && log.isWarnEnabled())
log.warn(_loc.get("override-query", name, currentLocation()));
QueryMetaData meta = getRepository().searchQueryMetaDataByName(name);
if (meta != null) {
Class defType = meta.getDefiningType();
if ((defType != _cls) && log.isWarnEnabled()) {
log.warn(_loc.get("dup-query", name, currentLocation(), defType));
}
pushElement(meta);
return true;
}
meta = getRepository().addQueryMetaData(null, name);
meta.setDefiningType(_cls);

View File

@ -34,7 +34,8 @@ dup-metadata: Found duplicate metadata or mapping for "{0}". Ignoring.
dup-sequence: Found duplicate generator "{0}" in "{1}". Ignoring.
override-sequence: Found duplicate generator "{0}" in "{1}". Overriding \
previous definition.
dup-query: Found duplicate query "{0}" in "{1}". Ignoring.
dup-query: Ignoring duplicate query "{0}" in "{1}". A query with the same name \
been already declared in "{2}".
override-query: Found duplicate query "{0}" in "{1}". Overriding previous \
definition.
no-seq-name: The sequence generator in "{0}" must declare a name.

View File

@ -111,6 +111,10 @@ public class TestQuery extends SliceTestCase {
em.getTransaction().rollback();
}
/**
* Retired temporarily. Most likely side-effect of eager compilation of
* query introduced recently.
*/
public void testHint() {
List<String> targets = new ArrayList<String>();
targets.add("Even");