Author: curtisr7
Date: Fri Oct 17 18:35:03 2014
New Revision: 1632647

URL: http://svn.apache.org/r1632647
Log:
OPENJPA-2533: Reorder MetaDataRepository call to fix a bug in orm resloution.

Added:
    
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/xml/
    
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/xml/TableNameInXmlEntity.java
   (with props)
    
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/xml/TestTableNameInXml.java
   (with props)
    
openjpa/trunk/openjpa-persistence-jdbc/src/test/resources/META-INF/table-orm.xml
   (with props)
Modified:
    
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java
    
openjpa/trunk/openjpa-persistence-jdbc/src/test/resources/META-INF/persistence.xml

Modified: 
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java?rev=1632647&r1=1632646&r2=1632647&view=diff
==============================================================================
--- 
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java
 (original)
+++ 
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java
 Fri Oct 17 18:35:03 2014
@@ -2000,17 +2000,16 @@ public class MetaDataRepository implemen
     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 implemen
         }
         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);
     }
 

Added: 
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/xml/TableNameInXmlEntity.java
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/xml/TableNameInXmlEntity.java?rev=1632647&view=auto
==============================================================================
--- 
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/xml/TableNameInXmlEntity.java
 (added)
+++ 
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/xml/TableNameInXmlEntity.java
 Fri Oct 17 18:35:03 2014
@@ -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;
+    }
+}

Propchange: 
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/xml/TableNameInXmlEntity.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/xml/TestTableNameInXml.java
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/xml/TestTableNameInXml.java?rev=1632647&view=auto
==============================================================================
--- 
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/xml/TestTableNameInXml.java
 (added)
+++ 
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/xml/TestTableNameInXml.java
 Fri Oct 17 18:35:03 2014
@@ -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";
+    }
+}

Propchange: 
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/xml/TestTableNameInXml.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: 
openjpa/trunk/openjpa-persistence-jdbc/src/test/resources/META-INF/persistence.xml
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/resources/META-INF/persistence.xml?rev=1632647&r1=1632646&r2=1632647&view=diff
==============================================================================
--- 
openjpa/trunk/openjpa-persistence-jdbc/src/test/resources/META-INF/persistence.xml
 (original)
+++ 
openjpa/trunk/openjpa-persistence-jdbc/src/test/resources/META-INF/persistence.xml
 Fri Oct 17 18:35:03 2014
@@ -497,7 +497,13 @@
                 value="buildSchema(ForeignKeys=true)"/>
                 
     </properties>
-        
-    </persistence-unit>      
+</persistence-unit>
+
+       <persistence-unit name="TableNameInXml-PU" 
transaction-type="RESOURCE_LOCAL">
+               <mapping-file>META-INF/table-orm.xml</mapping-file>
+               <properties>
+                       <property name="openjpa.jdbc.SynchronizeMappings" 
value="buildSchema(ForeignKeys=true)" />
+               </properties>
+       </persistence-unit>      
 
 </persistence>
\ No newline at end of file

Added: 
openjpa/trunk/openjpa-persistence-jdbc/src/test/resources/META-INF/table-orm.xml
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/resources/META-INF/table-orm.xml?rev=1632647&view=auto
==============================================================================
--- 
openjpa/trunk/openjpa-persistence-jdbc/src/test/resources/META-INF/table-orm.xml
 (added)
+++ 
openjpa/trunk/openjpa-persistence-jdbc/src/test/resources/META-INF/table-orm.xml
 Fri Oct 17 18:35:03 2014
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- 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. -->
+<entity-mappings version="2.0"
+       xmlns="http://java.sun.com/xml/ns/persistence/orm"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+       xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm 
http://java.sun.com/xml/ns/persistence/orm_2_0.xsd";>
+       <entity
+               
class="org.apache.openjpa.persistence.jdbc.query.xml.TableNameInXmlEntity">
+               <table name="TableNameInXml" />
+               <named-query name="query-in-xml">
+                       <query>SELECT t FROM TableNameInXmlEntity t</query>
+               </named-query>
+       </entity>
+</entity-mappings>

Propchange: 
openjpa/trunk/openjpa-persistence-jdbc/src/test/resources/META-INF/table-orm.xml
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to