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.HashMap;
020import java.util.Map;
021import java.util.logging.Level;
022
023import javax.persistence.EntityManager;
024import javax.persistence.spi.PersistenceProvider;
025
026import org.eclipse.persistence.config.PersistenceUnitProperties;
027import org.eclipse.persistence.config.TargetDatabase;
028import org.eclipse.persistence.jpa.JpaEntityManager;
029
030import org.springframework.lang.Nullable;
031
032/**
033 * {@link org.springframework.orm.jpa.JpaVendorAdapter} implementation for Eclipse
034 * Persistence Services (EclipseLink). Developed and tested against EclipseLink 2.7;
035 * backwards-compatible with EclipseLink 2.5 and 2.6 at runtime.
036 *
037 * <p>Exposes EclipseLink's persistence provider and EntityManager extension interface,
038 * and adapts {@link AbstractJpaVendorAdapter}'s common configuration settings.
039 * No support for the detection of annotated packages (through
040 * {@link org.springframework.orm.jpa.persistenceunit.SmartPersistenceUnitInfo#getManagedPackages()})
041 * since EclipseLink doesn't use package-level metadata.
042 *
043 * @author Juergen Hoeller
044 * @author Thomas Risberg
045 * @since 2.5.2
046 * @see EclipseLinkJpaDialect
047 * @see org.eclipse.persistence.jpa.PersistenceProvider
048 * @see org.eclipse.persistence.jpa.JpaEntityManager
049 */
050public class EclipseLinkJpaVendorAdapter extends AbstractJpaVendorAdapter {
051
052        private final PersistenceProvider persistenceProvider = new org.eclipse.persistence.jpa.PersistenceProvider();
053
054        private final EclipseLinkJpaDialect jpaDialect = new EclipseLinkJpaDialect();
055
056
057        @Override
058        public PersistenceProvider getPersistenceProvider() {
059                return this.persistenceProvider;
060        }
061
062        @Override
063        public Map<String, Object> getJpaPropertyMap() {
064                Map<String, Object> jpaProperties = new HashMap<>();
065
066                if (getDatabasePlatform() != null) {
067                        jpaProperties.put(PersistenceUnitProperties.TARGET_DATABASE, getDatabasePlatform());
068                }
069                else {
070                        String targetDatabase = determineTargetDatabaseName(getDatabase());
071                        if (targetDatabase != null) {
072                                jpaProperties.put(PersistenceUnitProperties.TARGET_DATABASE, targetDatabase);
073                        }
074                }
075
076                if (isGenerateDdl()) {
077                        jpaProperties.put(PersistenceUnitProperties.DDL_GENERATION,
078                                        PersistenceUnitProperties.CREATE_ONLY);
079                        jpaProperties.put(PersistenceUnitProperties.DDL_GENERATION_MODE,
080                                        PersistenceUnitProperties.DDL_DATABASE_GENERATION);
081                }
082                if (isShowSql()) {
083                        jpaProperties.put(PersistenceUnitProperties.CATEGORY_LOGGING_LEVEL_ +
084                                        org.eclipse.persistence.logging.SessionLog.SQL, Level.FINE.toString());
085                        jpaProperties.put(PersistenceUnitProperties.LOGGING_PARAMETERS, Boolean.TRUE.toString());
086                }
087
088                return jpaProperties;
089        }
090
091        /**
092         * Determine the EclipseLink target database name for the given database.
093         * @param database the specified database
094         * @return the EclipseLink target database name, or {@code null} if none found
095         */
096        @Nullable
097        protected String determineTargetDatabaseName(Database database) {
098                switch (database) {
099                        case DB2: return TargetDatabase.DB2;
100                        case DERBY: return TargetDatabase.Derby;
101                        case HANA: return TargetDatabase.HANA;
102                        case HSQL: return TargetDatabase.HSQL;
103                        case INFORMIX: return TargetDatabase.Informix;
104                        case MYSQL: return TargetDatabase.MySQL;
105                        case ORACLE: return TargetDatabase.Oracle;
106                        case POSTGRESQL: return TargetDatabase.PostgreSQL;
107                        case SQL_SERVER: return TargetDatabase.SQLServer;
108                        case SYBASE: return TargetDatabase.Sybase;
109                        default: return null;
110                }
111        }
112
113        @Override
114        public EclipseLinkJpaDialect getJpaDialect() {
115                return this.jpaDialect;
116        }
117
118        @Override
119        public Class<? extends EntityManager> getEntityManagerInterface() {
120                return JpaEntityManager.class;
121        }
122
123}