Guest User

Untitled

a guest
Apr 9th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. @Configuration
  2. @Profile("test")
  3. @EnableJpaRepositories(
  4. basePackages = "tracker.repository.wmx",
  5. entityManagerFactoryRef = "wmxH2EntityManager",
  6. transactionManagerRef = "wmxH2TransactionManager")
  7. public class WmxH2DatasourceConfig {
  8.  
  9. @Autowired
  10. private Environment env;
  11.  
  12. @Bean
  13. public LocalContainerEntityManagerFactoryBean wmxH2EntityManager() {
  14. LocalContainerEntityManagerFactoryBean em
  15. = new LocalContainerEntityManagerFactoryBean();
  16. em.setDataSource(wmxH2DataSource());
  17. em.setPackagesToScan(
  18. new String[]{"tracker.domain.wmx"});
  19.  
  20. HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
  21. em.setJpaVendorAdapter(vendorAdapter);
  22. HashMap<String, Object> properties = new HashMap<>();
  23. properties.put("hibernate.hbm2ddl.auto",
  24. env.getProperty("hibernate.hbm2ddl.auto"));
  25. properties.put("hibernate.dialect",
  26. env.getProperty("hibernate.dialect"));
  27. em.setJpaPropertyMap(properties);
  28.  
  29. return em;
  30. }
  31.  
  32. @Bean
  33. public DataSource wmxH2DataSource() {
  34.  
  35. DriverManagerDataSource dataSource
  36. = new DriverManagerDataSource();
  37. dataSource.setDriverClassName(
  38. env.getProperty("test.h2.datasource.driver-class-name"));
  39. dataSource.setUrl(env.getProperty("test.h2.datasource.url"));
  40. dataSource.setUsername(env.getProperty("test.h2.datasource.data-username"));
  41. dataSource.setPassword(env.getProperty("test.h2.datasource.data-password"));
  42.  
  43. return dataSource;
  44. }
  45.  
  46. @Bean
  47. public PlatformTransactionManager wmxH2TransactionManager() {
  48.  
  49. JpaTransactionManager transactionManager = new JpaTransactionManager();
  50. transactionManager.setEntityManagerFactory(wmxH2EntityManager().getObject());
  51. return transactionManager;
  52. }
  53.  
  54. }
  55.  
  56. @Configuration
  57. @Profile({"prod","test"})
  58. @EnableJpaRepositories(
  59. basePackages = "tracker.repository.h2",
  60. entityManagerFactoryRef = "h2EntityManager",
  61. transactionManagerRef = "h2TransactionManager")
  62. public class PrivateH2DatasourceConfig {
  63.  
  64. @Autowired
  65. private Environment env;
  66.  
  67. @Bean
  68. @Primary
  69. public LocalContainerEntityManagerFactoryBean h2EntityManager() {
  70. LocalContainerEntityManagerFactoryBean em
  71. = new LocalContainerEntityManagerFactoryBean();
  72. em.setDataSource(h2DataSource());
  73. em.setPackagesToScan(
  74. new String[]{"tracker.domain.h2"});
  75.  
  76. HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
  77. em.setJpaVendorAdapter(vendorAdapter);
  78. HashMap<String, Object> properties = new HashMap<>();
  79. properties.put("hibernate.hbm2ddl.auto",
  80. env.getProperty("hibernate.hbm2ddl.auto"));
  81. properties.put("hibernate.dialect",
  82. env.getProperty("hibernate.dialect"));
  83. em.setJpaPropertyMap(properties);
  84.  
  85. return em;
  86. }
  87.  
  88. @Bean
  89. @Primary
  90. public DataSource h2DataSource() {
  91.  
  92. DriverManagerDataSource dataSource
  93. = new DriverManagerDataSource();
  94. dataSource.setDriverClassName(
  95. env.getProperty("h2.datasource.driver-class-name"));
  96. dataSource.setUrl(env.getProperty("h2.datasource.url"));
  97. dataSource.setUsername(env.getProperty("h2.datasource.data-username"));
  98. dataSource.setPassword(env.getProperty("h2.datasource.data-password"));
  99.  
  100. return dataSource;
  101. }
  102.  
  103. @Bean
  104. @Primary
  105. public PlatformTransactionManager h2TransactionManager() {
  106.  
  107. JpaTransactionManager transactionManager = new JpaTransactionManager();
  108. transactionManager.setEntityManagerFactory(h2EntityManager().getObject());
  109. return transactionManager;
  110. }
  111.  
  112. }
  113.  
  114. #can be over ridden by run arguments
  115. spring.profiles.active=test
  116.  
  117. flyway.baseline-on-migrate=true
  118.  
  119.  
  120. ## H2
  121. h2.datasource.driver-class-name=org.h2.Driver
  122. h2.datasource.url=jdbc:h2:./data/deliverytracker.h2
  123. h2.datasource.data-username=x
  124. h2.datasource.data-password=xxx
  125.  
  126.  
  127. ## H2 WMX (Test environment)
  128. test.h2.datasource.driver-class-name=org.h2.Driver
  129. test.h2.datasource.url=jdbc:h2:./data/wmx_test.h2
  130. test.h2.datasource.data-username=x
  131. test.h2.datasource.data-password=xxx
  132. test.h2.datasource.initialize=true
  133. test.h2.datasource.data=classpath:schema-test.sql,classpath:data-test.sql
Add Comment
Please, Sign In to add comment