|
| 1 | +package com.baeldung.boot.config; |
| 2 | + |
| 3 | +import java.util.Properties; |
| 4 | + |
| 5 | +import javax.persistence.EntityManagerFactory; |
| 6 | +import javax.sql.DataSource; |
| 7 | + |
| 8 | +import org.springframework.beans.factory.annotation.Autowired; |
| 9 | +import org.springframework.context.annotation.Bean; |
| 10 | +import org.springframework.context.annotation.Configuration; |
| 11 | +import org.springframework.context.annotation.PropertySource; |
| 12 | +import org.springframework.core.env.Environment; |
| 13 | +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; |
| 14 | +import org.springframework.jdbc.datasource.DriverManagerDataSource; |
| 15 | +import org.springframework.orm.jpa.JpaTransactionManager; |
| 16 | +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; |
| 17 | +import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; |
| 18 | +import org.springframework.transaction.annotation.EnableTransactionManagement; |
| 19 | + |
| 20 | +@Configuration |
| 21 | +@EnableJpaRepositories(basePackages = { "org.baeldung.boot.repository", "org.baeldung.repository" }) |
| 22 | +@PropertySource("classpath:persistence-generic-entity.properties") |
| 23 | +@EnableTransactionManagement |
| 24 | +public class H2JpaConfig { |
| 25 | + |
| 26 | + @Autowired |
| 27 | + private Environment env; |
| 28 | + |
| 29 | + @Bean |
| 30 | + public DataSource dataSource() { |
| 31 | + final DriverManagerDataSource dataSource = new DriverManagerDataSource(); |
| 32 | + dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName")); |
| 33 | + dataSource.setUrl(env.getProperty("jdbc.url")); |
| 34 | + dataSource.setUsername(env.getProperty("jdbc.user")); |
| 35 | + dataSource.setPassword(env.getProperty("jdbc.pass")); |
| 36 | + |
| 37 | + return dataSource; |
| 38 | + } |
| 39 | + |
| 40 | + @Bean |
| 41 | + public LocalContainerEntityManagerFactoryBean entityManagerFactory() { |
| 42 | + final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); |
| 43 | + em.setDataSource(dataSource()); |
| 44 | + em.setPackagesToScan(new String[] { "org.baeldung.boot.domain" }); |
| 45 | + em.setJpaVendorAdapter(new HibernateJpaVendorAdapter()); |
| 46 | + em.setJpaProperties(additionalProperties()); |
| 47 | + return em; |
| 48 | + } |
| 49 | + |
| 50 | + @Bean |
| 51 | + JpaTransactionManager transactionManager(final EntityManagerFactory entityManagerFactory) { |
| 52 | + final JpaTransactionManager transactionManager = new JpaTransactionManager(); |
| 53 | + transactionManager.setEntityManagerFactory(entityManagerFactory); |
| 54 | + return transactionManager; |
| 55 | + } |
| 56 | + |
| 57 | + final Properties additionalProperties() { |
| 58 | + final Properties hibernateProperties = new Properties(); |
| 59 | + |
| 60 | + hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto")); |
| 61 | + hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect")); |
| 62 | + hibernateProperties.setProperty("hibernate.show_sql", env.getProperty("hibernate.show_sql")); |
| 63 | + |
| 64 | + return hibernateProperties; |
| 65 | + } |
| 66 | + |
| 67 | +} |
0 commit comments