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