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