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