The use of EntityListeners annotation in JPA

Usage scenarios

EntityListeners are used in jpa. If you are mybatis, you cannot use them

Its significance

Tracking the changes of entity attributes provides the status of before saving, after saving, before updating, after updating, before deleting and after deleting. Just like an interceptor, you can rewrite your personalized logic in the intercepting method.

Its use

Define interfaces, such as entity tracking

/**
 * Data creation and update
 */
public interface DataEntity {

  Timestamp getDateCreated();

  void setDateCreated(Timestamp dateCreated);

  Timestamp getLastUpdated();

  void setLastUpdated(Timestamp lastUpdated);

  Long getDateCreatedOn();

  void setDateCreatedOn(Long dateCreatedOn);

  Long getLastUpdatedOn();

  void setLastUpdatedOn(Long lastUpdatedOn);

}

Define tracker

@Slf4j
@Component
@Transactional
public class DataEntityListener {
  @PrePersist
  public void prePersist(DataEntity object)
      throws IllegalArgumentException, IllegalAccessException {
    Timestamp now = Timestamp.from(Instant.now());
    object.setDateCreated(now);
    object.setLastUpdated(now);
    logger.debug("save Previous operations");
  }

  @PostPersist
  public void postpersist(DataEntity object)
      throws IllegalArgumentException, IllegalAccessException {

    logger.debug("save Subsequent operations");
  }

  @PreUpdate
  public void preUpdate(DataEntity object)
      throws IllegalArgumentException, IllegalAccessException {
    Timestamp now = Timestamp.from(Instant.now());
    object.setLastUpdated(now);
    logger.debug("update Previous operations");
  }

  @PostUpdate
  public void postUpdate(DataEntity object)
      throws IllegalArgumentException, IllegalAccessException {
    logger.debug("update Subsequent operations");
  }

  @PreRemove
  public void preRemove(DataEntity object) {
    logger.debug("del Previous operations");

  }

  @PostRemove
  public void postRemove(DataEntity object) {
    logger.debug("del Subsequent operations");

  }
}

Entity to implement the corresponding trace interface

@EntityListeners(DataEntityListener.class)
public class Product implements DataEntity {
     @Override
  public Timestamp getDateCreated() {
    return createTime;
  }

  @Override
  public void setDateCreated(Timestamp dateCreated) {
    createTime = dateCreated;
  }

  @Override
  public Timestamp getLastUpdated() {
    return lastUpdateTime;
  }

  @Override
  public void setLastUpdated(Timestamp lastUpdated) {
    this.lastUpdateTime = lastUpdated;
  }

  @Override
  public Long getDateCreatedOn() {
    return createOn;
  }

  @Override
  public void setDateCreatedOn(Long dateCreatedOn) {
    createOn = dateCreatedOn;
  }

  @Override
  public Long getLastUpdatedOn() {
    return lastUpdateOn;
  }

  @Override
  public void setLastUpdatedOn(Long lastUpdatedOn) {
    this.lastUpdateOn = lastUpdatedOn;
  }
}

The above code will realize the assignment of createTime and lastUpdateTime when the entity is saved, and the reassignment of lastUpdateTime when the entity is updated.

Keywords: Java Mybatis

Added by pbsonawane on Sun, 01 Dec 2019 16:15:25 +0200