Spring Data JPA Auditing

David Winterfeldt

2012


This example shows how to use Spring Data JPA auditing. It sets up AOP based auditing for a create and last updated user & date.

1. Spring Configuration

The jpa:auditing element activates auditing and needs an instance of AuditorAware.

Excerpt from PersonRepositoryTest-context.xml

                
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/jdbc 
                           http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
                           http://www.springframework.org/schema/data/jpa
                           http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
    
    ...

    <!-- auditing -->
    <jpa:auditing auditor-aware-ref="auditorAware" />

    <bean id="auditorAware" class="org.springbyexample.orm.entity.AuditorAwareImpl" />

</beans>