179 lines
6.6 KiB
Java
Raw Normal View History

2016-01-20 21:04:35 -06:00
package com.baeldung.spring;
2013-05-04 22:36:14 +03:00
import java.util.Properties;
import javax.sql.DataSource;
2016-01-20 21:04:35 -06:00
import com.baeldung.persistence.dao.impl.BarDao;
import com.baeldung.persistence.dao.impl.FooDao;
import com.baeldung.persistence.service.IBarAuditableService;
import com.baeldung.persistence.service.IFooAuditableService;
import com.baeldung.persistence.service.IFooService;
2013-07-04 12:15:44 +03:00
import org.apache.tomcat.dbcp.dbcp.BasicDataSource;
2016-01-20 21:04:35 -06:00
import com.baeldung.persistence.dao.IBarAuditableDao;
import com.baeldung.persistence.dao.IBarDao;
import com.baeldung.persistence.dao.IFooAuditableDao;
import com.baeldung.persistence.dao.IFooDao;
import com.baeldung.persistence.dao.impl.BarAuditableDao;
import com.baeldung.persistence.dao.impl.BarJpaDao;
import com.baeldung.persistence.dao.impl.FooAuditableDao;
import com.baeldung.persistence.service.IBarService;
import com.baeldung.persistence.service.impl.BarAuditableService;
import com.baeldung.persistence.service.impl.BarJpaService;
import com.baeldung.persistence.service.impl.BarSpringDataJpaService;
import com.baeldung.persistence.service.impl.FooAuditableService;
import com.baeldung.persistence.service.impl.FooService;
2013-05-04 22:36:14 +03:00
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
2013-05-04 22:53:36 +03:00
import org.springframework.context.annotation.PropertySource;
2013-05-04 22:36:14 +03:00
import org.springframework.core.env.Environment;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
2013-05-04 22:36:14 +03:00
import org.springframework.orm.hibernate4.HibernateTransactionManager;
2013-05-04 22:53:36 +03:00
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
2013-05-04 22:36:14 +03:00
import org.springframework.transaction.annotation.EnableTransactionManagement;
2013-05-04 22:44:39 +03:00
import com.google.common.base.Preconditions;
2013-05-04 22:36:14 +03:00
@Configuration
@EnableTransactionManagement
2016-01-20 21:04:35 -06:00
@EnableJpaRepositories(basePackages = { "com.baeldung.persistence" }, transactionManagerRef = "jpaTransactionManager")
@EnableJpaAuditing
2013-05-04 22:53:36 +03:00
@PropertySource({ "classpath:persistence-mysql.properties" })
2016-01-20 21:04:35 -06:00
@ComponentScan({ "com.baeldung.persistence" })
2013-05-04 22:36:14 +03:00
public class PersistenceConfig {
@Autowired
private Environment env;
public PersistenceConfig() {
super();
}
@Bean
2013-05-04 22:53:36 +03:00
public LocalSessionFactoryBean sessionFactory() {
final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
2013-05-04 22:36:14 +03:00
sessionFactory.setDataSource(restDataSource());
2016-01-20 21:04:35 -06:00
sessionFactory.setPackagesToScan(new String[] { "com.baeldung.persistence.model" });
2013-05-04 22:36:14 +03:00
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
final LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
emf.setDataSource(restDataSource());
2016-01-20 21:04:35 -06:00
emf.setPackagesToScan(new String[] { "com.baeldung.persistence.model" });
final JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
emf.setJpaVendorAdapter(vendorAdapter);
emf.setJpaProperties(hibernateProperties());
return emf;
}
2013-05-04 22:36:14 +03:00
@Bean
public DataSource restDataSource() {
2013-07-04 12:15:44 +03:00
final BasicDataSource dataSource = new BasicDataSource();
2013-05-04 22:44:39 +03:00
dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName")));
dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url")));
dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("jdbc.user")));
dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("jdbc.pass")));
2013-05-04 22:36:14 +03:00
return dataSource;
}
@Bean
public PlatformTransactionManager hibernateTransactionManager() {
final HibernateTransactionManager transactionManager = new HibernateTransactionManager();
transactionManager.setSessionFactory(sessionFactory().getObject());
return transactionManager;
}
2013-05-04 22:36:14 +03:00
@Bean
public PlatformTransactionManager jpaTransactionManager() {
final JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
return transactionManager;
2013-05-04 22:36:14 +03:00
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
@Bean
public IBarService barJpaService() {
return new BarJpaService();
}
@Bean
public IBarService barSpringDataJpaService() {
return new BarSpringDataJpaService();
}
@Bean
public IFooService fooHibernateService() {
return new FooService();
}
@Bean
public IBarAuditableService barHibernateAuditableService() {
return new BarAuditableService();
}
@Bean
public IFooAuditableService fooHibernateAuditableService() {
return new FooAuditableService();
}
@Bean
public IBarDao barJpaDao() {
return new BarJpaDao();
}
@Bean
public IBarDao barHibernateDao() {
return new BarDao();
}
@Bean
public IBarAuditableDao barHibernateAuditableDao() {
return new BarAuditableDao();
}
@Bean
public IFooDao fooHibernateDao() {
return new FooDao();
}
@Bean
public IFooAuditableDao fooHibernateAuditableDao() {
return new FooAuditableDao();
}
private final Properties hibernateProperties() {
final Properties hibernateProperties = new Properties();
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
2014-04-09 22:06:54 +02:00
hibernateProperties.setProperty("hibernate.show_sql", "true");
// hibernateProperties.setProperty("hibernate.format_sql", "true");
// hibernateProperties.setProperty("hibernate.globally_quoted_identifiers", "true");
2014-04-09 22:06:54 +02:00
// Envers properties
hibernateProperties.setProperty("org.hibernate.envers.audit_table_suffix", env.getProperty("envers.audit_table_suffix"));
return hibernateProperties;
2013-05-04 22:36:14 +03:00
}
2013-05-04 22:36:14 +03:00
}