| /* |
| * Copyright 2017 The Mifos Initiative. |
| * |
| * Licensed 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 io.mifos.core.data.jpa.core; |
| |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| import org.springframework.context.annotation.Bean; |
| import org.springframework.context.annotation.ComponentScan; |
| import org.springframework.context.annotation.Configuration; |
| import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor; |
| import org.springframework.orm.jpa.JpaTransactionManager; |
| import org.springframework.orm.jpa.JpaVendorAdapter; |
| import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; |
| import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; |
| import org.springframework.transaction.PlatformTransactionManager; |
| import org.springframework.transaction.annotation.EnableTransactionManagement; |
| |
| import javax.persistence.EntityManagerFactory; |
| import javax.sql.DataSource; |
| |
| @Configuration |
| @EnableTransactionManagement |
| @ComponentScan({ |
| "**.core.data.jpa", |
| }) |
| public class DatabaseConnectorConfiguration { |
| public DatabaseConnectorConfiguration() { |
| super(); |
| } |
| |
| @Bean(name = DatabaseConnectorConstants.LOGGER_NAME) |
| public Logger logger() { |
| return LoggerFactory.getLogger(DatabaseConnectorConstants.LOGGER_NAME); |
| } |
| |
| @Bean |
| public LocalContainerEntityManagerFactoryBean entityManagerFactory(final DataSource dataSource) { |
| final LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean(); |
| entityManagerFactoryBean.setPersistenceUnitName("dynamicRoutingPU"); |
| entityManagerFactoryBean.setPackagesToScan("**.repository"); |
| entityManagerFactoryBean.setDataSource(dataSource); |
| |
| final JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); |
| entityManagerFactoryBean.setJpaVendorAdapter(vendorAdapter); |
| |
| return entityManagerFactoryBean; |
| } |
| |
| @Bean |
| public PlatformTransactionManager transactionManager(final EntityManagerFactory entityManagerFactory) { |
| final JpaTransactionManager transactionManager = new JpaTransactionManager(); |
| transactionManager.setEntityManagerFactory(entityManagerFactory); |
| return transactionManager; |
| } |
| |
| @Bean |
| public PersistenceExceptionTranslationPostProcessor exceptionTranslation() { |
| return new PersistenceExceptionTranslationPostProcessor(); |
| } |
| } |