Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate Envers with Spring Boot - configuration

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!

like image 788
Milan Avatar asked May 05 '14 10:05

Milan


People also ask

How do I use Hibernate Envers spring boot?

Hibernate Envers can be integrated very easyly with Spring Boot JPA. To use Envers in Spring boot application, We need to add below dependency. To Audit changes that are performed on an entity, we need to add @Audited annotation on the entity.

What is Envers?

The Envers module is a core Hibernate model that works both with Hibernate and JPA. In fact, you can use Envers anywhere Hibernate works whether that is standalone, inside WildFly or JBoss AS, Spring, Grails, etc. The Envers module aims to provide an easy auditing / versioning solution for entity classes.

How do you implement audit logs in spring boot?

Spring Boot JPA Audit Logging Example Spring Data helps you keep track of who created or modified an entity, as well as when it happened. To leverage this auditing functionality, you must provide auditing metadata to your entity classes, which can be defined using annotations or by implementing an interface.

What is spring Datavert?

Hibernate Envers project aimed to track data changes at the entity level with easy configurations in properties level and entity class level using annotations. The spring-data-envers project builds on top of Hibernate Envers and comes up as an extension of the Spring Data JPA project.


2 Answers

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
like image 179
M. Deinum Avatar answered Oct 08 '22 18:10

M. Deinum


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();     } } 
like image 22
Gabriel Ruiu Avatar answered Oct 08 '22 18:10

Gabriel Ruiu