diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ClassMappingInfo.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ClassMappingInfo.java index dd3a1643b..a22a4b9a1 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ClassMappingInfo.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ClassMappingInfo.java @@ -306,14 +306,8 @@ 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) { // delay this so that we don't do schema reflection for unique diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingInfo.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingInfo.java index c4b787aa5..4bf62f983 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingInfo.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingInfo.java @@ -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; diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/pudefaults/TestOpenJPASchemaPUDefault.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/pudefaults/TestOpenJPASchemaPUDefault.java new file mode 100644 index 000000000..c2a349f0c --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/pudefaults/TestOpenJPASchemaPUDefault.java @@ -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(); + } +} 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 85328bf69..da4ade91d 100644 --- a/openjpa-persistence-jdbc/src/test/resources/META-INF/persistence.xml +++ b/openjpa-persistence-jdbc/src/test/resources/META-INF/persistence.xml @@ -499,12 +499,22 @@ - - META-INF/table-orm.xml - - - - + + META-INF/pudefaults-orm.xml + META-INF/pudefaults2-orm.xml + + + + + + + + META-INF/table-orm.xml + + + +