From 08d94d75d68742fce002f65583844e5bb253b12e Mon Sep 17 00:00:00 2001 From: "Richard G. Curtis" Date: Fri, 17 Oct 2014 18:35:03 +0000 Subject: [PATCH] OPENJPA-2533: Reorder MetaDataRepository call to fix a bug in orm resloution. git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1632647 13f79535-47bb-0310-9956-ffa450edef68 --- .../openjpa/meta/MetaDataRepository.java | 21 ++--- .../jdbc/query/xml/TableNameInXmlEntity.java | 45 ++++++++++ .../jdbc/query/xml/TestTableNameInXml.java | 85 +++++++++++++++++++ .../test/resources/META-INF/persistence.xml | 10 ++- .../src/test/resources/META-INF/table-orm.xml | 22 +++++ 5 files changed, 168 insertions(+), 15 deletions(-) create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/xml/TableNameInXmlEntity.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/xml/TestTableNameInXml.java create mode 100644 openjpa-persistence-jdbc/src/test/resources/META-INF/table-orm.xml 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 9c08a30b0..2704ff3f0 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 @@ -2000,17 +2000,16 @@ public class MetaDataRepository implements PCRegistry.RegisterClassListener, Con private QueryMetaData getQueryMetaDataInternal(Class cls, String name, ClassLoader envLoader) { if (name == null) return null; - QueryMetaData qm = null; - if (cls == null) { - qm = searchQueryMetaDataByName(name); - if (qm != null) - return qm; - } + // check cache - qm = (QueryMetaData) _queries.get(name); + QueryMetaData qm = (QueryMetaData) _queries.get(name); if (qm != null) return qm; + // see if factory can figure out a scope for this query + if (cls == null) + cls = _factory.getQueryScope(name, envLoader); + // get metadata for class, which will find queries in metadata file if (cls != null && getMetaData(cls, envLoader, false) != null) { qm = _queries.get(name); @@ -2019,13 +2018,9 @@ public class MetaDataRepository implements PCRegistry.RegisterClassListener, Con } if ((_sourceMode & MODE_QUERY) == 0) return null; - - // see if factory can figure out a scope for this query - if (cls == null) - cls = _factory.getQueryScope(name, envLoader); - + // not in cache; load - _factory.load(cls, MODE_QUERY, envLoader); + _factory.load(cls, MODE_QUERY , envLoader); return _queries.get(name); } diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/xml/TableNameInXmlEntity.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/xml/TableNameInXmlEntity.java new file mode 100644 index 000000000..8a169e5e0 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/xml/TableNameInXmlEntity.java @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.persistence.jdbc.query.xml; + +import java.io.Serializable; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.NamedQuery; + +@Entity +@NamedQuery(name = "TableNameInXmlEntity.findAll", query = "SELECT t FROM TableNameInXmlEntity t") +public class TableNameInXmlEntity implements Serializable { + + private static final long serialVersionUID = 4508346087020196429L; + + @Id + private int myid; + + String a, b, c; + + public void setMyid(int myid) { + this.myid = myid; + } + + public int getMyid() { + return myid; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/xml/TestTableNameInXml.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/xml/TestTableNameInXml.java new file mode 100644 index 000000000..1cb727891 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/xml/TestTableNameInXml.java @@ -0,0 +1,85 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.persistence.jdbc.query.xml; + +import javax.persistence.EntityManager; +import javax.persistence.Query; + +import org.apache.openjpa.persistence.test.SQLListenerTestCase; + +public class TestTableNameInXml extends SQLListenerTestCase { + + String containsSQL = " FROM TableNameInXml "; + String notContainsSQL = " FROM TableNameInXmlEntity "; + + public void setUp() { + super.setUp(TableNameInXmlEntity.class); + } + + /* + * The SQL generated in this test should contain "FROM TableNameInXml" since the table name is defined in XML. + */ + public void testQuery() { + EntityManager em = emf.createEntityManager(); + + Query q = em.createQuery("SELECT t FROM TableNameInXmlEntity t"); + q.getResultList(); + assertContainsSQL(containsSQL); + assertNotSQL(notContainsSQL); + + em.close(); + } + + /* + * The SQL generated in this test should contain "FROM TableNameInXml" since the table name is defined in XML. Prior + * to OPENJPA-2533, the table name in XML was not being picked up. + */ + public void testNamedQuery() { + EntityManager em = emf.createEntityManager(); + + Query q = em.createNamedQuery("TableNameInXmlEntity.findAll"); + q.getResultList(); + assertContainsSQL(containsSQL); + assertNotSQL(notContainsSQL); + + em.close(); + } + + /* + * The SQL generated in this test should contain "FROM TableNameInXml" since the table name is defined in XML. This + * test works because the named query is executed second. + */ + public void testBoth() { + EntityManager em = emf.createEntityManager(); + + Query q = em.createQuery("SELECT t FROM TableNameInXmlEntity t"); + q.getResultList(); + + q = em.createNamedQuery("TableNameInXmlEntity.findAll"); + q.getResultList(); + assertContainsSQL(containsSQL); + assertNotSQL(notContainsSQL); + + em.close(); + } + + protected String getPersistenceUnitName() { + return "TableNameInXml-PU"; + } +} diff --git a/openjpa-persistence-jdbc/src/test/resources/META-INF/persistence.xml b/openjpa-persistence-jdbc/src/test/resources/META-INF/persistence.xml index 467b48917..bcb104ea6 100644 --- a/openjpa-persistence-jdbc/src/test/resources/META-INF/persistence.xml +++ b/openjpa-persistence-jdbc/src/test/resources/META-INF/persistence.xml @@ -497,7 +497,13 @@ value="buildSchema(ForeignKeys=true)"/> - - + + + + META-INF/table-orm.xml + + + + \ No newline at end of file diff --git a/openjpa-persistence-jdbc/src/test/resources/META-INF/table-orm.xml b/openjpa-persistence-jdbc/src/test/resources/META-INF/table-orm.xml new file mode 100644 index 000000000..7a4e977e4 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/resources/META-INF/table-orm.xml @@ -0,0 +1,22 @@ + + + + + + + SELECT t FROM TableNameInXmlEntity t + + +