Advertisement
Guest User

Untitled

a guest
Apr 1st, 2015
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.10 KB | None | 0 0
  1. package org.hemar.finance.service.config;
  2.  
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.context.annotation.DependsOn;
  6. import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
  7. import org.springframework.jdbc.datasource.DriverManagerDataSource;
  8. import org.springframework.orm.hibernate4.HibernateExceptionTranslator;
  9. import org.springframework.orm.jpa.JpaTransactionManager;
  10. import org.springframework.orm.jpa.JpaVendorAdapter;
  11. import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
  12. import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
  13. import org.springframework.transaction.PlatformTransactionManager;
  14. import org.springframework.transaction.annotation.EnableTransactionManagement;
  15. import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
  16.  
  17. import javax.persistence.EntityManagerFactory;
  18. import javax.sql.DataSource;
  19. import java.util.Properties;
  20.  
  21. /**
  22.  * Created by gadni on 20.11.2014..
  23.  */
  24. @Configuration
  25. @EnableJpaRepositories
  26. @EnableTransactionManagement
  27. public class PersistenceConfig {
  28.  
  29.     @Bean
  30.     public LocalValidatorFactoryBean validator() {
  31.         return new LocalValidatorFactoryBean();
  32.     }
  33.  
  34.     @Bean
  35.     public HibernateExceptionTranslator hibernateExceptionTranslator() {
  36.         return new HibernateExceptionTranslator();
  37.     }
  38.  
  39.     @Bean
  40.     public JpaVendorAdapter jpaVendorAdapter() {
  41.         return new HibernateJpaVendorAdapter();
  42.     }
  43.  
  44.  
  45.     @Bean
  46.     public DataSource dataSource() {
  47.         DriverManagerDataSource dataSource = new DriverManagerDataSource();
  48.  
  49.         dataSource.setDriverClassName("org.postgresql.Driver");
  50.         dataSource.setUrl("jdbc:postgresql://localhost:5432/finance");
  51.         dataSource.setUsername("user");
  52.         dataSource.setPassword("user");
  53.  
  54.         return dataSource;
  55.     }
  56.  
  57.     @Bean
  58.     @DependsOn("flyway")
  59.     public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
  60.         LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
  61.         factory.setJpaVendorAdapter(jpaVendorAdapter());
  62.         factory.setPackagesToScan("org.hemar.finance.service.persistence");
  63.         factory.setDataSource(dataSource());
  64.         factory.setJpaProperties(jpaProperties());
  65.         factory.afterPropertiesSet();
  66.  
  67.         return factory;
  68.     }
  69.  
  70.     @Bean
  71.     public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
  72.         JpaTransactionManager txManager = new JpaTransactionManager();
  73.         txManager.setEntityManagerFactory(entityManagerFactory);
  74.         return txManager;
  75.     }
  76.  
  77.     @Bean
  78.     public Properties jpaProperties() {
  79.         Properties jpaProperties = new Properties();
  80.         jpaProperties.setProperty("hibernate.hbm2ddl.auto", "create-drop");
  81.         jpaProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
  82.         jpaProperties.setProperty("hibernate.show_sql", "true");
  83.         return jpaProperties;
  84.     }
  85.  
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement