argos/dmp-backend/web/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;
2018-10-02 16:33:58 +02:00
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.*;
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")
2018-03-21 11:57:56 +01:00
@ComponentScan(basePackages = {"eu.eudat.data.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());
2018-03-21 11:57:56 +01:00
em.setPackagesToScan(new String[]{"eu.eudat.data.entities"});
2017-12-15 11:05:51 +01:00
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaProperties(additionalProperties());
return em;
}
@Bean
2018-10-02 16:33:58 +02:00
@Primary
@ConfigurationProperties(prefix = "spring.datasource")
2017-12-15 11:05:51 +01:00
public DataSource dataSource() {
2018-10-02 16:33:58 +02:00
return DataSourceBuilder
.create()
.username(env.getProperty("database.username"))
.password(env.getProperty("database.password"))
.url(env.getProperty("database.url"))
2018-10-02 16:33:58 +02:00
.driverClassName(env.getProperty("database.driver-class-name"))
.build();
2017-12-15 11:05:51 +01:00
}
@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-19 13:40:04 +01:00
properties.setProperty("hibernate.show_sql", "true");
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;
}
}