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 1b5845769..b2d0bfd3d 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 @@ -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. diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/SimpleEntity.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/SimpleEntity.java index 81b85f558..61b987d68 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/SimpleEntity.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/SimpleEntity.java @@ -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") }) diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/SimpleEntity2.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/SimpleEntity2.java index 920fd2a01..30f659410 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/SimpleEntity2.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/SimpleEntity2.java @@ -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") }) diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestDupNamedQuery.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestDupNamedQuery.java index cd1d7b37f..707925e27 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestDupNamedQuery.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestDupNamedQuery.java @@ -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) { diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/AnnotationPersistenceMetaDataParser.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/AnnotationPersistenceMetaDataParser.java index 2074400ea..1f9182b07 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/AnnotationPersistenceMetaDataParser.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/AnnotationPersistenceMetaDataParser.java @@ -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; } 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 5accd8db7..e6385225a 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 @@ -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); diff --git a/openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/localizer.properties b/openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/localizer.properties index b1e58e959..0c3c21809 100644 --- a/openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/localizer.properties +++ b/openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/localizer.properties @@ -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. diff --git a/openjpa-slice/src/test/java/org/apache/openjpa/slice/TestQuery.java b/openjpa-slice/src/test/java/org/apache/openjpa/slice/TestQuery.java index 1c0875348..6c2b3b625 100644 --- a/openjpa-slice/src/test/java/org/apache/openjpa/slice/TestQuery.java +++ b/openjpa-slice/src/test/java/org/apache/openjpa/slice/TestQuery.java @@ -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 targets = new ArrayList(); targets.add("Even");