Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.hemar.finance.service.config;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.context.annotation.DependsOn;
- import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
- import org.springframework.jdbc.datasource.DriverManagerDataSource;
- import org.springframework.orm.hibernate4.HibernateExceptionTranslator;
- 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 org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
- import javax.persistence.EntityManagerFactory;
- import javax.sql.DataSource;
- import java.util.Properties;
- /**
- * Created by gadni on 20.11.2014..
- */
- @Configuration
- @EnableJpaRepositories
- @EnableTransactionManagement
- public class PersistenceConfig {
- @Bean
- public LocalValidatorFactoryBean validator() {
- return new LocalValidatorFactoryBean();
- }
- @Bean
- public HibernateExceptionTranslator hibernateExceptionTranslator() {
- return new HibernateExceptionTranslator();
- }
- @Bean
- public JpaVendorAdapter jpaVendorAdapter() {
- return new HibernateJpaVendorAdapter();
- }
- @Bean
- public DataSource dataSource() {
- DriverManagerDataSource dataSource = new DriverManagerDataSource();
- dataSource.setDriverClassName("org.postgresql.Driver");
- dataSource.setUrl("jdbc:postgresql://localhost:5432/finance");
- dataSource.setUsername("user");
- dataSource.setPassword("user");
- return dataSource;
- }
- @Bean
- @DependsOn("flyway")
- public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
- LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
- factory.setJpaVendorAdapter(jpaVendorAdapter());
- factory.setPackagesToScan("org.hemar.finance.service.persistence");
- factory.setDataSource(dataSource());
- factory.setJpaProperties(jpaProperties());
- factory.afterPropertiesSet();
- return factory;
- }
- @Bean
- public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
- JpaTransactionManager txManager = new JpaTransactionManager();
- txManager.setEntityManagerFactory(entityManagerFactory);
- return txManager;
- }
- @Bean
- public Properties jpaProperties() {
- Properties jpaProperties = new Properties();
- jpaProperties.setProperty("hibernate.hbm2ddl.auto", "create-drop");
- jpaProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
- jpaProperties.setProperty("hibernate.show_sql", "true");
- return jpaProperties;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement