Hibernate Envers with Spring Boot - configuration
For all those configuration settings that aren't by default available you can specify them by simply prefixing them with spring.jpa.properties
. Those properties will be added, as is, to the EntityManagerFactory
(as JPA Properties).
spring.jpa.properties.org.hibernate.envers.default_schema=app_audit
Adding the above to the application.properties
will add the properties and should configure Hibernate Envers.
This is also documented in the Spring Boot reference guide.
Links
- Configure JPA properties
- Envers Properties
Looking through the HibernateJpaAutoConfiguration class I can't see any support for envers properties. The following might not be the best solution but nevertheless your can give it a try.
In order to have Spring Boot support the envers properties you have to:
override the current AutoConfiguration class that Spring Boot uses to configure the Hibernate properties, so it will read the envers properties from your property files. This will read the spring.jpa.hibernate.envers.default_schema from your file and add it to the properties of the entityManagerFactoryBean:
@Configuration public class HibernateEnversAutoConfiguration extends HibernateJpaAutoConfiguration { private RelaxedPropertyResolver environment; public HibernateEnversAutoConfiguration() { this.environment = null; } @Override public void setEnvironment(Environment environment) { super.setEnvironment(environment); this.environment = new RelaxedPropertyResolver(environment, "spring.jpa.hibernate."); } @Override protected void configure(LocalContainerEntityManagerFactoryBean entityManagerFactoryBean) { super.configure(entityManagerFactoryBean); Map<String, Object> properties = entityManagerFactoryBean.getJpaPropertyMap(); properties.put("hibernate.envers.default_schema", this.environment.getProperty("envers.default_schema")); } }
exclude the original HibernateJpaAutoConfiguration that Spring Boot uses and add your own as a bean so it will be replaced:
@EnableAutoConfiguration(exclude = HibernateJpaAutoConfiguration.class) @EnableJpaRepositories(basePackages = "com.gabrielruiu.test") @EntityScan(basePackages = "com.gabrielruiu.test") @ComponentScan(basePackages = "com.gabrielruiu.test") @Configuration public class Main { public static void main(String[] args) { SpringApplication.run(Main.class, args); } @Bean public HibernateEnversAutoConfiguration hibernateEnversAutoConfiguration() { return new HibernateEnversAutoConfiguration(); } }
I use with yaml format:
spring:
jpa:
properties:
org:
hibernate:
format_sql: false
envers:
audit_table_suffix: AUDIT
revision_field_name: NRO_ID_REVISAO_AUDITORIA
revision_type_field_name: TPO_REVISAO_AUDITORIA
For those using MySQL and Spring Boot, the suggestion of using:
spring.jpa.properties.org.hibernate.envers.default_schema=yourAuditSchema
will not work.
Use this instead:
spring.jpa.properties.org.hibernate.envers.default_catalog=yourAuditSchema