如何在jhipster中自定义AbstractAuditingEntity

时间:2016-05-18 10:20:57

标签: spring-boot jhipster

我想在 AbstractAuditingEntity 中添加与 id 相关的用户相关数据。

AbstractAuditingEntity 在创建的每个实体中添加createdDate等内容。我希望添加额外的实体,例如用户的 ID

因此,无论何时任何用户创建实体,他的ID都将与该记录一起添加,并与创建的日期一起添加。

1 个答案:

答案 0 :(得分:1)

您可以将以下类用作所有实体的父类

import java.io.Serializable;
import java.util.UUID;

import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Version;
import javax.validation.constraints.NotNull;

import org.joda.time.DateTime;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;

import com.skeleton.util.RequestContext;

/**
 * The parent class for all transactional persistent entities.
 * 
 * @author shijazi
 */
@MappedSuperclass // here is the important annotation
public class TransactionalEntity implements Serializable {

    /**
     * The default serial version UID.
     */
    private static final long serialVersionUID = 1L;

    /**
     * The primary key identifier.
     */
    @Id
    @GeneratedValue
    private Long id;

    /**
     * A secondary unique identifier which may be used as a reference to this
     * entity by external systems.
     */
    @NotNull
    private String referenceId = UUID.randomUUID().toString();

    /**
     * The entity instance version used for optimistic locking.
     */
    @Version
    private Integer version;

    /**
     * A reference to the entity or process which created this entity instance.
     */
    @NotNull
    private String createdBy;

    /**
     * The timestamp when this entity instance was created.
     */
    @NotNull
    private DateTime createdAt;

    /**
     * A reference to the entity or process which most recently updated this
     * entity instance.
     */
    private String updatedBy;

    /**
     * The timestamp when this entity instance was most recently updated.
     */
    private DateTime updatedAt;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getReferenceId() {
        return referenceId;
    }

    public void setReferenceId(String referenceId) {
        this.referenceId = referenceId;
    }

    public Integer getVersion() {
        return version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }

    public String getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

    public DateTime getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(DateTime createdAt) {
        this.createdAt = createdAt;
    }

    public String getUpdatedBy() {
        return updatedBy;
    }

    public void setUpdatedBy(String updatedBy) {
        this.updatedBy = updatedBy;
    }

    public DateTime getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(DateTime updatedAt) {
        this.updatedAt = updatedAt;
    }

    @PrePersist
    public void beforePersist() {

        setCreatedBy(username);

        setCreatedAt(new DateTime());
    }


    @PreUpdate
    public void beforeUpdate() {

        setUpdatedBy(username);

        setUpdatedAt(new DateTime());
    }
}

和实体如下:

 @Entity
 public class Person extends TransactionalEntity{

        @NotNull
        private String personId;

        private String arabicName;

        private String englishName;



        public String getPersonId() {
            return personId;
        }

        public void setPersonId(String personId) {
            this.personId = personId;
        }

        public String getArabicName() {
            return arabicName;
        }

        public void setArabicName(String arabicName) {
            this.arabicName = arabicName;
        }

        public String getEnglishName() {
            return englishName;
        }

        public void setEnglishName(String englishName) {
            this.englishName = englishName;
        }
}

我希望这个答案足够你了