Question

I'm trying to setup Hibernate Envers to work with my Spring Boot application.

I've included the Envers dependency and added @Audited annotations and it works fine, but I'm unable to configure specific Envers properties, Spring Boot doesn't seem to pick them up.

Specifically, I've tried to set the different db schema for audit tables by putting these to application.properties, but without luck:

hibernate.envers.default_schema=app_audit

or

org.hibernate.envers.default_schema=app_audit

or

spring.jpa.hibernate.envers.default_schema=app_audit

Neither of these work. Does anyone know how to set these?

EDIT.

As M. Deinum suggested I tried:

spring.jpa.properties.org.hibernate.envers.default_schema=app_audit

and it worked!

Was it helpful?

Solution

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

  1. Configure JPA properties
  2. Envers Properties

OTHER TIPS

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:

  1. 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"));
       }
    }
    
  2. 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();
        }
    }
    

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

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top