|
|
|
@@ -13,8 +13,10 @@ import org.springframework.context.annotation.PropertySource;
|
|
|
|
import org.springframework.core.env.Environment;
|
|
|
|
import org.springframework.core.env.Environment;
|
|
|
|
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
|
|
|
|
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
|
|
|
|
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
|
|
|
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
|
|
|
import org.springframework.orm.hibernate4.HibernateTransactionManager;
|
|
|
|
import org.springframework.orm.jpa.JpaTransactionManager;
|
|
|
|
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
|
|
|
|
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
|
|
|
|
|
|
|
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
|
|
|
|
|
|
|
import org.springframework.transaction.PlatformTransactionManager;
|
|
|
|
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
|
|
|
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
|
|
|
|
|
|
|
|
|
|
|
import com.google.common.base.Preconditions;
|
|
|
|
import com.google.common.base.Preconditions;
|
|
|
|
@@ -34,17 +36,21 @@ public class PersistenceConfig {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
@Bean
|
|
|
|
public LocalSessionFactoryBean sessionFactory() {
|
|
|
|
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() {
|
|
|
|
final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
|
|
|
|
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
|
|
|
sessionFactory.setDataSource(restDataSource());
|
|
|
|
em.setDataSource(dataSource());
|
|
|
|
sessionFactory.setPackagesToScan(new String[] { "org.baeldung.spring.persistence.model" });
|
|
|
|
em.setPackagesToScan(new String[] { "org.baeldung.persistence.model" });
|
|
|
|
sessionFactory.setHibernateProperties(hibernateProperties());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return sessionFactory;
|
|
|
|
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
|
|
|
|
|
|
|
// vendorAdapter.set
|
|
|
|
|
|
|
|
em.setJpaVendorAdapter(vendorAdapter);
|
|
|
|
|
|
|
|
em.setJpaProperties(additionalProperties());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return em;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
@Bean
|
|
|
|
public DataSource restDataSource() {
|
|
|
|
public DataSource dataSource() {
|
|
|
|
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
|
|
|
|
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
|
|
|
|
dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName")));
|
|
|
|
dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName")));
|
|
|
|
dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url")));
|
|
|
|
dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url")));
|
|
|
|
@@ -55,11 +61,11 @@ public class PersistenceConfig {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
@Bean
|
|
|
|
public HibernateTransactionManager transactionManager() {
|
|
|
|
public PlatformTransactionManager transactionManager() {
|
|
|
|
final HibernateTransactionManager txManager = new HibernateTransactionManager();
|
|
|
|
final JpaTransactionManager transactionManager = new JpaTransactionManager();
|
|
|
|
txManager.setSessionFactory(sessionFactory().getObject());
|
|
|
|
transactionManager.setEntityManagerFactory(entityManagerFactoryBean().getObject());
|
|
|
|
|
|
|
|
|
|
|
|
return txManager;
|
|
|
|
return transactionManager;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
@Bean
|
|
|
|
@@ -67,7 +73,7 @@ public class PersistenceConfig {
|
|
|
|
return new PersistenceExceptionTranslationPostProcessor();
|
|
|
|
return new PersistenceExceptionTranslationPostProcessor();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
final Properties hibernateProperties() {
|
|
|
|
final Properties additionalProperties() {
|
|
|
|
return new Properties() {
|
|
|
|
return new Properties() {
|
|
|
|
{
|
|
|
|
{
|
|
|
|
setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
|
|
|
|
setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
|
|
|
|
@@ -78,4 +84,5 @@ public class PersistenceConfig {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|