2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.controlloop.runtime.config;
23 import java.util.HashMap;
25 import javax.persistence.EntityManagerFactory;
26 import javax.sql.DataSource;
27 import org.eclipse.persistence.config.BatchWriting;
28 import org.eclipse.persistence.config.PersistenceUnitProperties;
29 import org.eclipse.persistence.logging.SessionLog;
30 import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup;
31 import org.springframework.beans.factory.ObjectProvider;
32 import org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration;
33 import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
34 import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
35 import org.springframework.context.annotation.Bean;
36 import org.springframework.context.annotation.Configuration;
37 import org.springframework.context.annotation.Primary;
38 import org.springframework.orm.jpa.JpaTransactionManager;
39 import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
40 import org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter;
41 import org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter;
42 import org.springframework.transaction.PlatformTransactionManager;
43 import org.springframework.transaction.annotation.EnableTransactionManagement;
44 import org.springframework.transaction.jta.JtaTransactionManager;
47 @EnableTransactionManagement
48 public class JpaConfiguration extends JpaBaseConfiguration {
50 protected JpaConfiguration(DataSource dataSource, JpaProperties properties,
51 ObjectProvider<JtaTransactionManager> jtaTransactionManager) {
52 super(dataSource, properties, jtaTransactionManager);
56 protected AbstractJpaVendorAdapter createJpaVendorAdapter() {
57 return new EclipseLinkJpaVendorAdapter();
61 protected Map<String, Object> getVendorProperties() {
62 return Map.of(PersistenceUnitProperties.BATCH_WRITING, BatchWriting.JDBC);
66 * Create EntityManagerFactory.
68 * @param builder EntityManagerFactoryBuilder
69 * @param dataSource DataSource
70 * @return LocalContainerEntityManagerFactoryBean
72 @Bean("entityManagerFactory")
73 public LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactory(
74 EntityManagerFactoryBuilder builder, DataSource dataSource,
75 ClRuntimeParameterGroup clRuntimeParameterGroup) {
77 return builder.dataSource(dataSource)
78 .persistenceUnit(clRuntimeParameterGroup.getDatabaseProviderParameters().getPersistenceUnit())
79 .properties(initJpaProperties()).build();
83 * create a PlatformTransactionManager.
85 * @param emf EntityManagerFactory
86 * @return PlatformTransactionManager
89 public static PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
90 final var transactionManager = new JpaTransactionManager();
91 transactionManager.setEntityManagerFactory(emf);
92 return transactionManager;
96 * create Jpa Properties.
98 * @return JpaProperties
102 public static JpaProperties properties(ClRuntimeParameterGroup clRuntimeParameterGroup) {
103 final var jpaProperties = new JpaProperties();
104 jpaProperties.setShowSql(clRuntimeParameterGroup.isShowSql());
105 jpaProperties.setDatabasePlatform(clRuntimeParameterGroup.getDatabasePlatform());
106 return jpaProperties;
109 private static Map<String, ?> initJpaProperties() {
110 final Map<String, Object> ret = new HashMap<>();
111 // Add any JpaProperty you are interested in and is supported by your Database and JPA implementation
112 ret.put(PersistenceUnitProperties.BATCH_WRITING, BatchWriting.JDBC);
113 ret.put(PersistenceUnitProperties.LOGGING_LEVEL, SessionLog.INFO_LABEL);
114 ret.put(PersistenceUnitProperties.WEAVING, "false");
115 ret.put(PersistenceUnitProperties.DDL_GENERATION, PersistenceUnitProperties.CREATE_ONLY);
116 ret.put(PersistenceUnitProperties.DDL_GENERATION_MODE, PersistenceUnitProperties.DDL_DATABASE_GENERATION);