OPENJPA-2704: The openjpa.jdbc.Schema no longer overrides orm.xml default

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1834900 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Will Dazey 2018-07-02 21:07:29 +00:00
parent 0303fb28f8
commit 4477ace6fd
4 changed files with 94 additions and 15 deletions

View File

@ -306,13 +306,7 @@ public class ClassMappingInfo
* Return the named table for the given class.
*/
public Table getTable(final ClassMapping cls, DBIdentifier tableName,
boolean adapt) {
// If the schemaName is NULL type then check for a system default schema name
// and if available use it.
if (_schemaName != null && _schemaName.getType() == DBIdentifierType.NULL){
String name = cls.getMappingRepository().getMetaDataFactory().getDefaults().getDefaultSchema();
_schemaName = (name != null ? DBIdentifier.newSchema(name) : _schemaName);
}
boolean adapt) {
Table t = createTable(cls, new TableDefaults() {
public String get(Schema schema) {

View File

@ -29,6 +29,7 @@ import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
import org.apache.openjpa.jdbc.identifier.Normalizer;
import org.apache.openjpa.jdbc.identifier.DBIdentifier;
import org.apache.openjpa.jdbc.identifier.QualifiedDBIdentifier;
import org.apache.openjpa.jdbc.identifier.DBIdentifier.DBIdentifierType;
import org.apache.openjpa.jdbc.schema.Column;
import org.apache.openjpa.jdbc.schema.ColumnIO;
import org.apache.openjpa.jdbc.schema.ForeignKey;
@ -499,10 +500,19 @@ public abstract class MappingInfo
&& !repos.getMappingDefaults().defaultMissingInfo())))
throw new MetaDataException(_loc.get("no-table", context));
if (DBIdentifier.isNull(schemaName))
if (DBIdentifier.isNull(schemaName)) {
//Check the configuration first for a set Schema to use
schemaName = Schemas.getNewTableSchemaIdentifier((JDBCConfiguration)
repos.getConfiguration());
// If the schemaName is still NULL type then check for a system default schema name
// and if available use it.
if (schemaName != null && (schemaName.getType() == DBIdentifierType.NULL)) {
String name = repos.getMetaDataFactory().getDefaults().getDefaultSchema();
schemaName = (name != null ? DBIdentifier.newSchema(name) : schemaName);
}
}
// if no given and adapting or defaulting missing info, use template
SchemaGroup group = repos.getSchemaGroup();
Schema schema = null;

View File

@ -0,0 +1,65 @@
/*
* 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.pudefaults;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import org.apache.openjpa.persistence.test.SQLListenerTestCase;
/*
* OPENJPA-2704: These tests expand on TestSchemaPUDefault to verify
* that a schema defined in an orm's persistence-unit-default is
* overriden by the "openjpa.jdbc.Schema" property .
*/
public class TestOpenJPASchemaPUDefault extends SQLListenerTestCase {
public void setUp() throws Exception {
super.setUp(PUDefaultSchemaEntity.class, PUSchemaInSequenceAnnotationEntity.class,
PUSchemaInTableAnnotationEntity.class, PUSchemaInTableMappingEntity.class,
PUSchemaInSequenceMappingEntity.class);
setSupportedDatabases(org.apache.openjpa.jdbc.sql.DB2Dictionary.class);
}
public void tearDown() throws Exception {
super.tearDown();
}
@Override
protected String getPersistenceUnitName() {
return "overrideMappingSchema";
}
public void testOpenJPASchemaOverridesORM() {
persist(new PUDefaultSchemaEntity());
// The Sequence and Table SQL should use the PU default schema
assertContainsSQL("ALTER SEQUENCE PUSCHEMA.SeqName_4DefaultSchema");
assertContainsSQL("INSERT INTO PUSCHEMA.PUDefaultSchemaEntity");
}
public void persist(Object ent){
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
em.persist(ent);
tx.commit();
em.close();
}
}

View File

@ -499,12 +499,22 @@
</properties>
</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-unit name="overrideMappingSchema" transaction-type="RESOURCE_LOCAL">
<mapping-file>META-INF/pudefaults-orm.xml</mapping-file>
<mapping-file>META-INF/pudefaults2-orm.xml</mapping-file>
<properties>
<property name="openjpa.jdbc.Schema" value="PUSCHEMA" />
<property name="openjpa.jdbc.SynchronizeMappings"
value="buildSchema(ForeignKeys=true)"/>
</properties>
</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-unit name="TestPUDefaultCascadePersist" transaction-type="RESOURCE_LOCAL">
<!-- <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider> -->