Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.nikedlab.cxs.config
- import java.util.Properties
- import javax.persistence.EntityManagerFactory
- import javax.sql.DataSource
- import org.flywaydb.core.Flyway
- import org.springframework.boot.autoconfigure.flyway.FlywayDataSource
- import org.springframework.context.annotation.{Bean, Configuration}
- import org.springframework.jdbc.datasource.DriverManagerDataSource
- import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter
- import org.springframework.orm.jpa.{JpaTransactionManager, JpaVendorAdapter, LocalContainerEntityManagerFactoryBean}
- import org.springframework.transaction.PlatformTransactionManager
- import org.springframework.transaction.annotation.EnableTransactionManagement
- /**
- * Created by NikedLab
- * 22.11.16.
- */
- @Configuration
- @EnableTransactionManagement
- class JpaConfig {
- @Bean
- def entityManagerFactory(): LocalContainerEntityManagerFactoryBean = {
- val em: LocalContainerEntityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean
- em.setDataSource(dataSource)
- em.setPackagesToScan("com.nikedlab.cxs")
- val vendorAdapter: JpaVendorAdapter = new HibernateJpaVendorAdapter
- em.setJpaVendorAdapter(vendorAdapter)
- em.setJpaProperties(additionalProperties)
- em
- }
- def additionalProperties: Properties = {
- val properties: Properties = new Properties()
- properties.setProperty("hibernate.hbm2ddl.auto", "validate")
- properties.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQL9Dialect")
- properties.setProperty("hibernate.show_sql", "true")
- properties
- }
- @Bean
- def dataSource: DataSource = {
- val dataSource: DriverManagerDataSource = new DriverManagerDataSource()
- dataSource.setDriverClassName("org.postgresql.Driver")
- dataSource.setUrl("jdbc:postgresql://localhost:5432/currency")
- dataSource.setUsername("currency")
- dataSource.setPassword("currency")
- dataSource
- }
- @Bean(initMethod = "migrate")
- @FlywayDataSource
- def flyway(): Flyway = {
- val flyway: Flyway = new Flyway
- flyway.setDataSource(dataSource)
- flyway.setTable("platform_schema_version")
- flyway.setBaselineOnMigrate(true)
- flyway.setLocations("classpath:/db/migration2")
- flyway.setValidateOnMigrate(false)
- flyway
- }
- def transactionManager(emf: EntityManagerFactory): PlatformTransactionManager = {
- val transactionManager: JpaTransactionManager = new JpaTransactionManager()
- transactionManager.setEntityManagerFactory(emf)
- transactionManager
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement