|
| 1 | +package org.baeldung.spring; |
| 2 | + |
| 3 | +import java.util.Properties; |
| 4 | + |
| 5 | +import javax.sql.DataSource; |
| 6 | + |
| 7 | +import org.apache.tomcat.dbcp.dbcp2.BasicDataSource; |
| 8 | +import org.hibernate.SessionFactory; |
| 9 | +import org.springframework.beans.factory.annotation.Autowired; |
| 10 | +import org.springframework.context.annotation.Bean; |
| 11 | +import org.springframework.context.annotation.ComponentScan; |
| 12 | +import org.springframework.context.annotation.Configuration; |
| 13 | +import org.springframework.context.annotation.PropertySource; |
| 14 | +import org.springframework.core.env.Environment; |
| 15 | +import org.springframework.core.io.ClassPathResource; |
| 16 | +import org.springframework.core.io.Resource; |
| 17 | +import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor; |
| 18 | +import org.springframework.orm.hibernate3.HibernateTransactionManager; |
| 19 | +import org.springframework.orm.hibernate3.LocalSessionFactoryBean; |
| 20 | +import org.springframework.transaction.annotation.EnableTransactionManagement; |
| 21 | + |
| 22 | +import com.google.common.base.Preconditions; |
| 23 | + |
| 24 | +@Configuration |
| 25 | +@EnableTransactionManagement |
| 26 | +@PropertySource({ "classpath:persistence-h2.properties" }) |
| 27 | +@ComponentScan({ "org.baeldung.persistence.dao", "org.baeldung.persistence.service" }) |
| 28 | +public class PersistenceConfigHibernate3 { |
| 29 | + |
| 30 | + @Autowired |
| 31 | + private Environment env; |
| 32 | + |
| 33 | + public PersistenceConfigHibernate3() { |
| 34 | + super(); |
| 35 | + } |
| 36 | + |
| 37 | + @Bean |
| 38 | + public LocalSessionFactoryBean sessionFactory() { |
| 39 | + final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean(); |
| 40 | + Resource config = new ClassPathResource("exceptionDemo.cfg.xml"); |
| 41 | + sessionFactory.setDataSource(dataSource()); |
| 42 | + sessionFactory.setConfigLocation(config); |
| 43 | + sessionFactory.setHibernateProperties(hibernateProperties()); |
| 44 | + |
| 45 | + return sessionFactory; |
| 46 | + } |
| 47 | + |
| 48 | + @Bean |
| 49 | + public DataSource dataSource() { |
| 50 | + final BasicDataSource dataSource = new BasicDataSource(); |
| 51 | + dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName"))); |
| 52 | + dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url"))); |
| 53 | + dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("jdbc.user"))); |
| 54 | + dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("jdbc.pass"))); |
| 55 | + |
| 56 | + return dataSource; |
| 57 | + } |
| 58 | + |
| 59 | + @Bean |
| 60 | + @Autowired |
| 61 | + public HibernateTransactionManager transactionManager(final SessionFactory sessionFactory) { |
| 62 | + final HibernateTransactionManager txManager = new HibernateTransactionManager(); |
| 63 | + txManager.setSessionFactory(sessionFactory); |
| 64 | + |
| 65 | + return txManager; |
| 66 | + } |
| 67 | + |
| 68 | + @Bean |
| 69 | + public PersistenceExceptionTranslationPostProcessor exceptionTranslation() { |
| 70 | + return new PersistenceExceptionTranslationPostProcessor(); |
| 71 | + } |
| 72 | + |
| 73 | + final Properties hibernateProperties() { |
| 74 | + final Properties hibernateProperties = new Properties(); |
| 75 | + hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto")); |
| 76 | + hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect")); |
| 77 | + // hibernateProperties.setProperty("hibernate.globally_quoted_identifiers", "true"); |
| 78 | + return hibernateProperties; |
| 79 | + } |
| 80 | + |
| 81 | +} |
0 commit comments