Εμφάνιση αναρτήσεων με ετικέτα LocalContainerEntityManagerFactoryBean. Εμφάνιση όλων των αναρτήσεων
Εμφάνιση αναρτήσεων με ετικέτα LocalContainerEntityManagerFactoryBean. Εμφάνιση όλων των αναρτήσεων

Τρίτη 16 Μαρτίου 2021

Configure Spring datasource with Hibernate JpaTransactionManager and LocalContainerEntityManagerFactoryBean



Configure Spring DataSource with Hibernate 







1. Configuration class


@Configuration @EnableTransactionManagement public class JpaConfiguration { @Value("${spring.jpa.database-platform}") private String databasePlatform; @Value("${spring.jpa.hibernate.ddl-auto}") private String autoDdlCreation; @Bean public PlatformTransactionManager transactionManager() { JpaTransactionManager transactionManager = new JpaTransactionManager(); transactionManager.setEntityManagerFactory(entityManagerFactory().getObject()); return transactionManager; } @Bean public LocalContainerEntityManagerFactoryBean entityManagerFactory() { LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); em.setDataSource(someDatabaseDS()); em.setPackagesToScan(new String[] {"your.app.directory.for.persistence"}); JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); em.setJpaVendorAdapter(vendorAdapter); em.setJpaProperties(additionalProperties()); return em; } @Bean @Primary @ConfigurationProperties("some-db.datasource") public DataSource someDatabaseDS() { return DataSourceBuilder.create().build(); } Properties additionalProperties() { Properties properties = new Properties(); properties.setProperty("hibernate.dialect", databasePlatform); properties.setProperty("hibernate.show_sql", "false"); properties.setProperty("hibernate.hbm2ddl.auto", autoDdlCreation); return properties; } }



2. Application yml / properties


spring:
  jpa:
    database-platform: org.hibernate.dialect.Oracle10gDialect
    hibernate:
      ddl-auto: none

some-db:
  datasource:
    jdbcUrl: "someUrl"
    username: "someUserName"
    password: "somePassword"
    maximumPoolSize: 20