Advertisement
NikedLab

Untitled

Feb 9th, 2017
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. package com.nikedlab.cxs.config
  2.  
  3. import java.util.Properties
  4. import javax.persistence.EntityManagerFactory
  5. import javax.sql.DataSource
  6.  
  7. import org.flywaydb.core.Flyway
  8. import org.springframework.boot.autoconfigure.flyway.FlywayDataSource
  9. import org.springframework.context.annotation.{Bean, Configuration}
  10. import org.springframework.jdbc.datasource.DriverManagerDataSource
  11. import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter
  12. import org.springframework.orm.jpa.{JpaTransactionManager, JpaVendorAdapter, LocalContainerEntityManagerFactoryBean}
  13. import org.springframework.transaction.PlatformTransactionManager
  14. import org.springframework.transaction.annotation.EnableTransactionManagement
  15.  
  16. /**
  17. * Created by NikedLab
  18. * 22.11.16.
  19. */
  20. @Configuration
  21. @EnableTransactionManagement
  22. class JpaConfig {
  23.  
  24. @Bean
  25. def entityManagerFactory(): LocalContainerEntityManagerFactoryBean = {
  26. val em: LocalContainerEntityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean
  27. em.setDataSource(dataSource)
  28. em.setPackagesToScan("com.nikedlab.cxs")
  29. val vendorAdapter: JpaVendorAdapter = new HibernateJpaVendorAdapter
  30. em.setJpaVendorAdapter(vendorAdapter)
  31. em.setJpaProperties(additionalProperties)
  32. em
  33. }
  34.  
  35. def additionalProperties: Properties = {
  36. val properties: Properties = new Properties()
  37. properties.setProperty("hibernate.hbm2ddl.auto", "validate")
  38. properties.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQL9Dialect")
  39. properties.setProperty("hibernate.show_sql", "true")
  40. properties
  41. }
  42.  
  43. @Bean
  44. def dataSource: DataSource = {
  45. val dataSource: DriverManagerDataSource = new DriverManagerDataSource()
  46. dataSource.setDriverClassName("org.postgresql.Driver")
  47. dataSource.setUrl("jdbc:postgresql://localhost:5432/currency")
  48. dataSource.setUsername("currency")
  49. dataSource.setPassword("currency")
  50. dataSource
  51. }
  52.  
  53. @Bean(initMethod = "migrate")
  54. @FlywayDataSource
  55. def flyway(): Flyway = {
  56. val flyway: Flyway = new Flyway
  57. flyway.setDataSource(dataSource)
  58. flyway.setTable("platform_schema_version")
  59. flyway.setBaselineOnMigrate(true)
  60. flyway.setLocations("classpath:/db/migration2")
  61. flyway.setValidateOnMigrate(false)
  62. flyway
  63. }
  64.  
  65. def transactionManager(emf: EntityManagerFactory): PlatformTransactionManager = {
  66. val transactionManager: JpaTransactionManager = new JpaTransactionManager()
  67. transactionManager.setEntityManagerFactory(emf)
  68. transactionManager
  69. }
  70.  
  71.  
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement