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
This commit is contained in:
Richard G. Curtis 2014-10-17 18:35:03 +00:00
parent 078da9d00f
commit 08d94d75d6
5 changed files with 168 additions and 15 deletions

View File

@ -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);
}

View File

@ -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;
}
}

View File

@ -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";
}
}

View File

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

View File

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