package org.javers.hibernate.integration.config;
import com.google.common.collect.ImmutableMap;
import org.javers.core.Javers;
import org.javers.repository.sql.ConnectionProvider;
import org.javers.repository.sql.JaversSqlRepository;
import org.javers.spring.auditable.AuthorProvider;
import org.javers.spring.auditable.CommitPropertiesProvider;
import org.javers.spring.auditable.aspect.JaversAuditableAspect;
import org.javers.spring.auditable.aspect.springdata.JaversSpringDataAuditableRepositoryAspect;
import org.javers.spring.jpa.JpaHibernateConnectionProvider;
import org.springframework.context.annotation.Bean;
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 javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.Map;
import java.util.Properties;
public class HibernateConfig {
public static final String H2_URL = "jdbc:h2:mem:test";
/**
* Integrates {@link JaversSqlRepository} with Spring {@link JpaTransactionManager}
*/
@Bean
public ConnectionProvider jpaConnectionProvider() {
return new JpaHibernateConnectionProvider();
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan("org.javers.hibernate");
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaProperties(additionalProperties());
return em;
}
@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("org.h2.Driver");
dataSource.setUrl(H2_URL + ";DB_CLOSE_DELAY=-1");
return dataSource;
}
@Bean
public JaversAuditableAspect javersAuditableAspect(Javers javers) {
return new JaversAuditableAspect(javers, authorProvider(), commitPropertiesProvider());
}
@Bean
public JaversSpringDataAuditableRepositoryAspect javersSpringDataAuditableAspect(Javers javers) {
return new JaversSpringDataAuditableRepositoryAspect(javers, authorProvider(), commitPropertiesProvider());
}
/**
* Required by Repository auto-audit aspect. <br/><br/>
* <p>
* Returns mock implementation for testing.
* <br/>
* Provide real implementation,
* when using Spring Security you can use
* {@link org.javers.spring.auditable.SpringSecurityAuthorProvider}.
*/
@Bean
public AuthorProvider authorProvider() {
return new AuthorProvider() {
@Override
public String provide() {
return "unknown";
}
};
}
@Bean
public CommitPropertiesProvider commitPropertiesProvider() {
return new CommitPropertiesProvider() {
@Override
public Map<String, String> provide() {
return ImmutableMap.of("key", "ok");
}
};
}
Properties additionalProperties() {
Properties properties = new Properties();
properties.setProperty("hibernate.hbm2ddl.auto", "create");
properties.setProperty("hibernate.connection.autocommit", "false");
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
properties.setProperty("hibernate.current_session_context_class", "thread");
properties.setProperty("hibernate.enable_lazy_load_no_trans", "true");
return properties;
}
}