72 lines
2.8 KiB
Java
72 lines
2.8 KiB
Java
|
package eu.eudat.configurations;
|
||
|
|
||
|
/**
|
||
|
* Created by ikalyvas on 12/15/2017.
|
||
|
*/
|
||
|
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;
|
||
|
import org.springframework.core.env.Environment;
|
||
|
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
|
||
|
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||
|
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;
|
||
|
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||
|
import javax.persistence.EntityManagerFactory;
|
||
|
import javax.sql.DataSource;
|
||
|
import java.util.Properties;
|
||
|
|
||
|
@Configuration
|
||
|
@EnableTransactionManagement
|
||
|
@ComponentScan(basePackages = {"eu.eudat.entities"})
|
||
|
public class DatabaseConfiguration {
|
||
|
|
||
|
@Autowired
|
||
|
private Environment env;
|
||
|
|
||
|
@Bean
|
||
|
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
|
||
|
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
||
|
em.setDataSource(dataSource());
|
||
|
em.setPackagesToScan(new String[]{"eu.eudat.entities"});
|
||
|
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
||
|
em.setJpaVendorAdapter(vendorAdapter);
|
||
|
em.setJpaProperties(additionalProperties());
|
||
|
|
||
|
return em;
|
||
|
}
|
||
|
|
||
|
@Bean
|
||
|
public DataSource dataSource() {
|
||
|
DriverManagerDataSource dataSource = new DriverManagerDataSource();
|
||
|
dataSource.setDriverClassName(env.getProperty("database.driver-class-name"));
|
||
|
dataSource.setUrl(env.getProperty("database.url"));
|
||
|
dataSource.setUsername(env.getProperty("database.username"));
|
||
|
dataSource.setPassword(env.getProperty("database.password"));
|
||
|
return dataSource;
|
||
|
}
|
||
|
|
||
|
@Bean
|
||
|
public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
|
||
|
JpaTransactionManager transactionManager = new JpaTransactionManager();
|
||
|
transactionManager.setEntityManagerFactory(emf);
|
||
|
|
||
|
return transactionManager;
|
||
|
}
|
||
|
|
||
|
@Bean
|
||
|
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
|
||
|
return new PersistenceExceptionTranslationPostProcessor();
|
||
|
}
|
||
|
|
||
|
private Properties additionalProperties() {
|
||
|
Properties properties = new Properties();
|
||
|
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQL92Dialect");
|
||
|
properties.setProperty("hibernate.show_sql", "true");
|
||
|
return properties;
|
||
|
}
|
||
|
}
|