package com.github.marschall.threeten.jpa.h2.configuration;
import static org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType.H2;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.orm.jpa.JpaDialect;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect;
import org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
@Configuration
public class H2Configuration {
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()
.setType(H2)
.addScript("h2-schema.sql")
.addScript("h2-data.sql")
.build();
}
@Bean
public PlatformTransactionManager txManager(EntityManagerFactory entityManagerFactory) {
JpaTransactionManager transactionManager = new JpaTransactionManager(entityManagerFactory);
transactionManager.setDataSource(dataSource());
transactionManager.setJpaDialect(jpaDialect());
return transactionManager;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManager(DataSource dataSource) {
LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean();
bean.setPersistenceUnitName("threeten-jpa-eclipselink-h2");
bean.setJpaDialect(jpaDialect());
bean.setJpaVendorAdapter(new EclipseLinkJpaVendorAdapter());
bean.setDataSource(dataSource);
return bean;
}
@Bean
public JpaDialect jpaDialect() {
return new EclipseLinkJpaDialect();
}
}