|
| 1 | +package com.baeldung.manytomany.spring; |
| 2 | + |
| 3 | +import java.util.Properties; |
| 4 | +import javax.sql.DataSource; |
| 5 | +import org.apache.tomcat.dbcp.dbcp2.BasicDataSource; |
| 6 | +import org.springframework.beans.factory.annotation.Autowired; |
| 7 | +import org.springframework.context.annotation.Bean; |
| 8 | +import org.springframework.context.annotation.ComponentScan; |
| 9 | +import org.springframework.context.annotation.Configuration; |
| 10 | +import org.springframework.context.annotation.PropertySource; |
| 11 | +import org.springframework.core.env.Environment; |
| 12 | +import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor; |
| 13 | +import org.springframework.orm.hibernate4.HibernateTransactionManager; |
| 14 | +import org.springframework.orm.hibernate4.LocalSessionFactoryBean; |
| 15 | +import org.springframework.transaction.PlatformTransactionManager; |
| 16 | +import org.springframework.transaction.annotation.EnableTransactionManagement; |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +@Configuration |
| 21 | +@EnableTransactionManagement |
| 22 | +@PropertySource({ "classpath:persistence-mysql.properties" }) |
| 23 | +@ComponentScan({ "com.baeldung.hibernate.manytomany" }) |
| 24 | +public class PersistenceConfig { |
| 25 | + |
| 26 | + @Autowired |
| 27 | + private Environment env; |
| 28 | + |
| 29 | + @Bean |
| 30 | + public LocalSessionFactoryBean sessionFactory() { |
| 31 | + final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean(); |
| 32 | + sessionFactory.setDataSource(restDataSource()); |
| 33 | + sessionFactory.setPackagesToScan(new String[] { "com.baeldung.hibernate.manytomany" }); |
| 34 | + sessionFactory.setHibernateProperties(hibernateProperties()); |
| 35 | + |
| 36 | + return sessionFactory; |
| 37 | + } |
| 38 | + |
| 39 | + @Bean |
| 40 | + public DataSource restDataSource() { |
| 41 | + final BasicDataSource dataSource = new BasicDataSource(); |
| 42 | + dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName")); |
| 43 | + dataSource.setUrl(env.getProperty("jdbc.url")); |
| 44 | + dataSource.setUsername(env.getProperty("jdbc.user")); |
| 45 | + dataSource.setPassword(env.getProperty("jdbc.pass")); |
| 46 | + |
| 47 | + return dataSource; |
| 48 | + } |
| 49 | + |
| 50 | + @Bean |
| 51 | + public PlatformTransactionManager hibernateTransactionManager() { |
| 52 | + final HibernateTransactionManager transactionManager = new HibernateTransactionManager(); |
| 53 | + transactionManager.setSessionFactory(sessionFactory().getObject()); |
| 54 | + return transactionManager; |
| 55 | + } |
| 56 | + |
| 57 | + @Bean |
| 58 | + public PersistenceExceptionTranslationPostProcessor exceptionTranslation() { |
| 59 | + return new PersistenceExceptionTranslationPostProcessor(); |
| 60 | + } |
| 61 | + |
| 62 | + private final Properties hibernateProperties() { |
| 63 | + final Properties hibernateProperties = new Properties(); |
| 64 | + //hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto")); |
| 65 | + hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect")); |
| 66 | + hibernateProperties.setProperty("hibernate.show_sql", "true"); |
| 67 | + |
| 68 | + return hibernateProperties; |
| 69 | + } |
| 70 | +} |
0 commit comments