Java tutorial
/* * This file is subject to the terms and conditions defined in file 'LICENSE.md', * which is part of this source code package. */ package com.greendot.db.jpa.configuration; import org.apache.commons.dbcp2.BasicDataSource; import org.springframework.context.EnvironmentAware; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.core.env.Environment; import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.JpaVendorAdapter; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor; 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; import java.util.Properties; /** * @author matt.d.vickery@greendotsoftware.co.uk * @since 10/25/16. */ @Configuration @PropertySource(value = { "classpath:properties/database-${spring.profiles.active:default}.properties" }) @EnableTransactionManagement public class JpaDatabaseConfiguration implements EnvironmentAware { private Environment environment; @Override public void setEnvironment(final Environment environment) { this.environment = environment; } @Bean public PersistenceAnnotationBeanPostProcessor persistenceAnnotationBeanPostProcessor() { return new PersistenceAnnotationBeanPostProcessor(); } @Bean public DataSource dataSource() { final BasicDataSource dataSource = new org.apache.commons.dbcp2.BasicDataSource(); dataSource.setDriverClassName(environment.getProperty("db.datasource.driver")); dataSource.setUrl(environment.getProperty("db.datasource.url")); dataSource.setUsername(environment.getProperty("db.datasource.username")); dataSource.setPassword(environment.getProperty("db.datasource.password")); return dataSource; } @Bean public LocalContainerEntityManagerFactoryBean hibernateBackedJpaEntityManagerFactory() { final LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean(); entityManagerFactory.setDataSource(dataSource()); entityManagerFactory.setPersistenceUnitName(environment.getProperty("db.persistenceUnitName")); entityManagerFactory.setPackagesToScan(environment.getProperty("db.entityPackageToScan")); final JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); entityManagerFactory.setJpaVendorAdapter(vendorAdapter); entityManagerFactory.setJpaProperties(jpaHibernateProperties()); return entityManagerFactory; } @Bean public PlatformTransactionManager transactionManager(final EntityManagerFactory entityManagerFactory) { final JpaTransactionManager transactionManager = new JpaTransactionManager(); transactionManager.setEntityManagerFactory(entityManagerFactory); return transactionManager; } private Properties jpaHibernateProperties() { final Properties properties = new Properties(); properties.setProperty("hibernate.hbm2ddl.auto", environment.getProperty("db.hibernate.hbm2ddl.auto")); properties.setProperty("hibernate.dialect", environment.getProperty("db.hibernate.dialect")); return properties; } }