3. Code Example

Example 1. AuditorAwareImpl

This is a very simple example that just returns a static value.

[Note]Note

If Spring Security was setup, the method could return the current user.

                        
return SecurityContextHolder.getContext().getAuthentication().getName();
                
                    
                
public class AuditorAwareImpl implements AuditorAware<String> {

    @Override
    public String getCurrentAuditor() {
        return "SYSTEM";
    }


}
                
            

Example 2. AbstractAuditableEntity

This auditing class extends Spring Data JPA's AbstractPersistable, which has an auto-increment primary key field in it and some utility methods. The Auditable interface uses generics to take the user and it's primary key type.

[Note]Note

If the reference of AuditorAware was setup to be a user entity, then Spring Data JPA's AbstractAuditable could be used as the entity base.

Notice the @MappedSuperclass annotation. It indicates it is designated to have it's field mappings used by subclasses.

                
@MappedSuperclass
@SuppressWarnings("serial")
public class AbstractAuditableEntity extends AbstractPersistable<Integer> implements Auditable<String, Integer> {

    private DateTime lastUpdated;
    private String lastUpdateUser;
    private DateTime created;
    private String createUser;
    
    /**
     * Gets created by audit user.
     */
    @Override
    public String getCreatedBy() {
        return createUser;
    }

    /**
     * Sets created by audit user.
     */
    @Override
    public void setCreatedBy(String createdBy) {
        this.createUser = createdBy;
    }
    
    /**
     * Gets create audit date.
     */    
    @Override
    public DateTime getCreatedDate() {
        return created;
    }

    /**
     * Sets create audit date.
     */    
    @Override
    public void setCreatedDate(DateTime creationDate) {
        this.created = creationDate;
    }

    /**
     * Gets last modified by audit user.
     */
    @Override
    public String getLastModifiedBy() {
        return lastUpdateUser;
    }
    
    /**
     * Sets last modified by audit user.
     */
    @Override
    public void setLastModifiedBy(String lastModifiedBy) {
        this.lastUpdateUser = lastModifiedBy;
    }

    /**
     * Gets last modified audit date.
     */    
    @Override
    public DateTime getLastModifiedDate() {
        return lastUpdated;
    }

    /**
     * Sets last modified audit date.
     */    
    @Override
    public void setLastModifiedDate(DateTime lastModifiedDate) {
        this.lastUpdated = lastModifiedDate;
    }

}
                
            

Example 3. Excerpt from Person

The Person class extends AbstractAuditableEntity. By doing that it gets a primary key and audit fields, along with getters/setters for the inherited fields, equals, and hashCode. Only the fields that are specific to this type need to be defined.

                
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name="TYPE", discriminatorType=DiscriminatorType.INTEGER)
public class Person extends AbstractAuditableEntity {

    private static final long serialVersionUID = -2175150694352541150L;

    private String firstName = null;
    private String lastName = null;

    @OneToMany(fetch=FetchType.EAGER, cascade=CascadeType.ALL)
    @JoinColumn(name="PERSON_ID", nullable=false)
    private Set<Address> addresses = null;

    ...
    
}