argos/dmp-backend/src/main/java/eu/eudat/configurations/DevelDatabaseConfiguration....

74 lines
2.9 KiB
Java
Raw Normal View History

2017-12-15 11:05:51 +01:00
package eu.eudat.configurations;
2018-02-01 10:08:06 +01:00
2017-12-15 11:05:51 +01: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;
2018-02-09 16:54:41 +01:00
import org.springframework.context.annotation.Profile;
2017-12-15 11:05:51 +01:00
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;
2018-02-16 11:34:02 +01:00
2017-12-15 11:05:51 +01:00
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.Properties;
@Configuration
@EnableTransactionManagement
2018-02-09 16:54:41 +01:00
@Profile("devel")
2017-12-15 11:05:51 +01:00
@ComponentScan(basePackages = {"eu.eudat.entities"})
2018-02-09 16:54:41 +01:00
public class DevelDatabaseConfiguration {
2017-12-15 11:05:51 +01:00
@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"));
2018-02-09 16:54:41 +01:00
dataSource.setUrl(env.getProperty("devel.database.url"));
dataSource.setUsername(env.getProperty("devel.database.username"));
dataSource.setPassword(env.getProperty("devel.database.password"));
2017-12-15 11:05:51 +01:00
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");
2018-03-05 17:18:45 +01:00
properties.setProperty("hibernate.show_sql", "false");
2018-02-16 11:34:02 +01:00
properties.setProperty("hibernate.temp.use_jdbc_metadata_defaults", "false");
2017-12-15 11:05:51 +01:00
return properties;
}
}