001/*
002 * Copyright 2002-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.orm.jpa.vendor;
018
019import java.util.Collections;
020import java.util.Map;
021
022import javax.persistence.EntityManager;
023import javax.persistence.EntityManagerFactory;
024import javax.persistence.spi.PersistenceUnitInfo;
025
026import org.springframework.lang.Nullable;
027import org.springframework.orm.jpa.JpaDialect;
028import org.springframework.orm.jpa.JpaVendorAdapter;
029
030/**
031 * Abstract {@link JpaVendorAdapter} implementation that defines common properties,
032 * to be translated into vendor-specific JPA properties by concrete subclasses.
033 *
034 * @author Juergen Hoeller
035 * @author Rod Johnson
036 * @since 2.0
037 */
038public abstract class AbstractJpaVendorAdapter implements JpaVendorAdapter {
039
040        private Database database = Database.DEFAULT;
041
042        @Nullable
043        private String databasePlatform;
044
045        private boolean generateDdl = false;
046
047        private boolean showSql = false;
048
049
050        /**
051         * Specify the target database to operate on, as a value of the {@code Database} enum:
052         * DB2, DERBY, H2, HANA, HSQL, INFORMIX, MYSQL, ORACLE, POSTGRESQL, SQL_SERVER, SYBASE
053         * <p><b>NOTE:</b> This setting will override your JPA provider's default algorithm.
054         * Custom vendor properties may still fine-tune the database dialect. However,
055         * there may nevertheless be conflicts: For example, specify either this setting
056         * or Hibernate's "hibernate.dialect_resolvers" property, not both.
057         */
058        public void setDatabase(Database database) {
059                this.database = database;
060        }
061
062        /**
063         * Return the target database to operate on.
064         */
065        protected Database getDatabase() {
066                return this.database;
067        }
068
069        /**
070         * Specify the name of the target database to operate on.
071         * The supported values are vendor-dependent platform identifiers.
072         */
073        public void setDatabasePlatform(@Nullable String databasePlatform) {
074                this.databasePlatform = databasePlatform;
075        }
076
077        /**
078         * Return the name of the target database to operate on.
079         */
080        @Nullable
081        protected String getDatabasePlatform() {
082                return this.databasePlatform;
083        }
084
085        /**
086         * Set whether to generate DDL after the EntityManagerFactory has been initialized,
087         * creating/updating all relevant tables.
088         * <p>Note that the exact semantics of this flag depend on the underlying
089         * persistence provider. For any more advanced needs, specify the appropriate
090         * vendor-specific settings as "jpaProperties".
091         * <p><b>NOTE: Do not set this flag to 'true' while also setting JPA 2.1's
092         * {@code javax.persistence.schema-generation.database.action} property.</b>
093         * These two schema generation mechanisms - standard JPA versus provider-native -
094         * are mutually exclusive, e.g. with Hibernate 5.
095         * @see org.springframework.orm.jpa.AbstractEntityManagerFactoryBean#setJpaProperties
096         */
097        public void setGenerateDdl(boolean generateDdl) {
098                this.generateDdl = generateDdl;
099        }
100
101        /**
102         * Return whether to generate DDL after the EntityManagerFactory has been initialized
103         * creating/updating all relevant tables.
104         */
105        protected boolean isGenerateDdl() {
106                return this.generateDdl;
107        }
108
109        /**
110         * Set whether to show SQL in the log (or in the console).
111         * <p>For more specific logging configuration, specify the appropriate
112         * vendor-specific settings as "jpaProperties".
113         * @see org.springframework.orm.jpa.AbstractEntityManagerFactoryBean#setJpaProperties
114         */
115        public void setShowSql(boolean showSql) {
116                this.showSql = showSql;
117        }
118
119        /**
120         * Return whether to show SQL in the log (or in the console).
121         */
122        protected boolean isShowSql() {
123                return this.showSql;
124        }
125
126
127        @Override
128        @Nullable
129        public String getPersistenceProviderRootPackage() {
130                return null;
131        }
132
133        @Override
134        public Map<String, ?> getJpaPropertyMap(PersistenceUnitInfo pui) {
135                return getJpaPropertyMap();
136        }
137
138        @Override
139        public Map<String, ?> getJpaPropertyMap() {
140                return Collections.emptyMap();
141        }
142
143        @Override
144        @Nullable
145        public JpaDialect getJpaDialect() {
146                return null;
147        }
148
149        @Override
150        public Class<? extends EntityManagerFactory> getEntityManagerFactoryInterface() {
151                return EntityManagerFactory.class;
152        }
153
154        @Override
155        public Class<? extends EntityManager> getEntityManagerInterface() {
156                return EntityManager.class;
157        }
158
159        @Override
160        public void postProcessEntityManagerFactory(EntityManagerFactory emf) {
161        }
162
163}