|
| 1 | +package es.msanchez.frameworks.java.spring.boot.config; |
| 2 | + |
| 3 | +import com.zaxxer.hikari.HikariConfig; |
| 4 | +import com.zaxxer.hikari.HikariDataSource; |
| 5 | +import org.springframework.context.annotation.Bean; |
| 6 | +import org.springframework.context.annotation.Configuration; |
| 7 | +import org.springframework.core.env.Environment; |
| 8 | +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; |
| 9 | +import org.springframework.orm.jpa.JpaTransactionManager; |
| 10 | +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; |
| 11 | +import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; |
| 12 | +import org.springframework.transaction.annotation.EnableTransactionManagement; |
| 13 | + |
| 14 | +import javax.persistence.EntityManagerFactory; |
| 15 | +import javax.sql.DataSource; |
| 16 | +import java.util.Properties; |
| 17 | + |
| 18 | +@Configuration |
| 19 | +@EnableJpaRepositories(basePackages = { |
| 20 | + "es.msanchez.frameworks.java.spring.boot.**" |
| 21 | +}) |
| 22 | +@EnableTransactionManagement |
| 23 | +public class DatabaseConfig { |
| 24 | + |
| 25 | + @Bean |
| 26 | + DataSource dataSource(final Environment env) { |
| 27 | + final HikariConfig dataSourceConfig = new HikariConfig(); |
| 28 | + dataSourceConfig.setDriverClassName(env.getRequiredProperty("db.driver")); |
| 29 | + dataSourceConfig.setJdbcUrl(env.getRequiredProperty("db.url")); |
| 30 | + dataSourceConfig.setUsername(env.getRequiredProperty("db.username")); |
| 31 | + dataSourceConfig.setPassword(env.getRequiredProperty("db.password")); |
| 32 | + return new HikariDataSource(dataSourceConfig); |
| 33 | + } |
| 34 | + |
| 35 | + @Bean |
| 36 | + LocalContainerEntityManagerFactoryBean entityManagerFactory(final DataSource dataSource, |
| 37 | + final Environment env) { |
| 38 | + final LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean(); |
| 39 | + entityManagerFactoryBean.setDataSource(dataSource); |
| 40 | + entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter()); |
| 41 | + entityManagerFactoryBean.setPackagesToScan("es.msanchez.frameworks.java.spring.boot.**"); |
| 42 | + |
| 43 | + final Properties jpaProperties = new Properties(); |
| 44 | + |
| 45 | + //Configures the used database dialect. This allows Hibernate to create SQL |
| 46 | + //that is optimized for the used database. |
| 47 | + jpaProperties.put("hibernate.dialect", env.getRequiredProperty("hibernate.dialect")); |
| 48 | + |
| 49 | + //Specifies the action that is invoked to the database when the Hibernate |
| 50 | + //SessionFactory is created or closed. |
| 51 | + jpaProperties.put("hibernate.hbm2ddl.auto", |
| 52 | + env.getRequiredProperty("hibernate.hbm2ddl.auto") |
| 53 | + ); |
| 54 | + |
| 55 | + //Configures the naming strategy that is used when Hibernate creates |
| 56 | + //new database objects and schema elements |
| 57 | + jpaProperties.put("hibernate.ejb.naming_strategy", |
| 58 | + env.getRequiredProperty("hibernate.ejb.naming_strategy") |
| 59 | + ); |
| 60 | + |
| 61 | + //If the value of this property is true, Hibernate writes all SQL |
| 62 | + //statements to the console. |
| 63 | + jpaProperties.put("hibernate.show_sql", |
| 64 | + env.getRequiredProperty("hibernate.show_sql") |
| 65 | + ); |
| 66 | + |
| 67 | + //If the value of this property is true, Hibernate will format the SQL |
| 68 | + //that is written to the console. |
| 69 | + jpaProperties.put("hibernate.format_sql", |
| 70 | + env.getRequiredProperty("hibernate.format_sql") |
| 71 | + ); |
| 72 | + |
| 73 | + entityManagerFactoryBean.setJpaProperties(jpaProperties); |
| 74 | + |
| 75 | + return entityManagerFactoryBean; |
| 76 | + } |
| 77 | + |
| 78 | + @Bean |
| 79 | + JpaTransactionManager transactionManager(final EntityManagerFactory entityManagerFactory) { |
| 80 | + final JpaTransactionManager transactionManager = new JpaTransactionManager(); |
| 81 | + transactionManager.setEntityManagerFactory(entityManagerFactory); |
| 82 | + return transactionManager; |
| 83 | + } |
| 84 | + |
| 85 | +} |
0 commit comments