Talk about the changes in the Spring Data Auditable interface.

  springcloud

Order

This article mainly studies the changes of the Spring Data Auditable interface.

1.12.8.RELEASE version

spring-data-commons-1.12.8.RELEASE-sources.jar! /org/springframework/data/domain/Auditable.java

import java.io.Serializable;

import org.joda.time.DateTime;

/**
 * Interface for auditable entities. Allows storing and retrieving creation and modification information. The changing
 * instance (typically some user) is to be defined by a generics definition.
 * 
 * @param <U> the auditing type. Typically some kind of user.
 * @param <ID> the type of the audited type's identifier
 * @author Oliver Gierke
 */
public interface Auditable<U, ID extends Serializable> extends Persistable<ID> {

    /**
     * Returns the user who created this entity.
     * 
     * @return the createdBy
     */
    U getCreatedBy();

    /**
     * Sets the user who created this entity.
     * 
     * @param createdBy the creating entity to set
     */
    void setCreatedBy(final U createdBy);

    /**
     * Returns the creation date of the entity.
     * 
     * @return the createdDate
     */
    DateTime getCreatedDate();

    /**
     * Sets the creation date of the entity.
     * 
     * @param creationDate the creation date to set
     */
    void setCreatedDate(final DateTime creationDate);

    /**
     * Returns the user who modified the entity lastly.
     * 
     * @return the lastModifiedBy
     */
    U getLastModifiedBy();

    /**
     * Sets the user who modified the entity lastly.
     * 
     * @param lastModifiedBy the last modifying entity to set
     */
    void setLastModifiedBy(final U lastModifiedBy);

    /**
     * Returns the date of the last modification.
     * 
     * @return the lastModifiedDate
     */
    DateTime getLastModifiedDate();

    /**
     * Sets the date of the last modification.
     * 
     * @param lastModifiedDate the date of the last modification to set
     */
    void setLastModifiedDate(final DateTime lastModifiedDate);
}

You can see the DateTime of joda-time used in this version.

2.0.7.RELEASE version

spring-data-commons-2.0.7.RELEASE-sources.jar! /org/springframework/data/domain/Auditable.java

/**
 * Interface for auditable entities. Allows storing and retrieving creation and modification information. The changing
 * instance (typically some user) is to be defined by a generics definition.
 * 
 * @param <U> the auditing type. Typically some kind of user.
 * @param <ID> the type of the audited type's identifier
 * @author Oliver Gierke
 */
public interface Auditable<U, ID, T extends TemporalAccessor> extends Persistable<ID> {

    /**
     * Returns the user who created this entity.
     * 
     * @return the createdBy
     */
    Optional<U> getCreatedBy();

    /**
     * Sets the user who created this entity.
     * 
     * @param createdBy the creating entity to set
     */
    void setCreatedBy(U createdBy);

    /**
     * Returns the creation date of the entity.
     * 
     * @return the createdDate
     */
    Optional<T> getCreatedDate();

    /**
     * Sets the creation date of the entity.
     * 
     * @param creationDate the creation date to set
     */
    void setCreatedDate(T creationDate);

    /**
     * Returns the user who modified the entity lastly.
     * 
     * @return the lastModifiedBy
     */
    Optional<U> getLastModifiedBy();

    /**
     * Sets the user who modified the entity lastly.
     * 
     * @param lastModifiedBy the last modifying entity to set
     */
    void setLastModifiedBy(U lastModifiedBy);

    /**
     * Returns the date of the last modification.
     * 
     * @return the lastModifiedDate
     */
    Optional<T> getLastModifiedDate();

    /**
     * Sets the date of the last modification.
     * 
     * @param lastModifiedDate the date of the last modification to set
     */
    void setLastModifiedDate(T lastModifiedDate);
}

You can see the new version, which removes the mandatory dependence on joda-time and instead adds generics to the interface definition to express it. the generics require the implementation of the TemporalAccessor interface.
In addition, the type of return value has been changed to Optional.

Summary

The new Auditable has two major changes:

  • Changing the time type to generic definition requires specific types to implement the TemporalAccessor interface
  • The return value uniformly returns the Optional type.

doc