@Component
public class MegaAuditingEntityListener{
@Autowired
private ObjectFactory handler;
/**
* Sets modification and creation date and auditor on the target object in case it implements {@link Auditable} on
* persist events.
*
* @param target
*/
@PrePersist
public void touchForPreCreate(Object target) {
Assert.notNull(target, "Entity must not be null!");
if (handler != null) {
AuditingHandler object = handler.getObject();
if (object != null) {
object.markCreated(target);
}
if (target instanceof BaseEntity) {
//更新工作空间到基础字段里面
String workspaceCode = Optional.ofNullable(((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getHeader(WORKSPACE_CODE_HEADER)).orElse("molclone");
BaseEntity baseEntity = (BaseEntity) target;
baseEntity.setWorkspaceCode(workspaceCode);
//更新租户ID到基础字段里面
String tenantId = Optional.ofNullable(((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getHeader(HEADER_TENANT_ID)).orElse("-1");
baseEntity.setTenantId(tenantId);
}
}
}
/**
* Sets modification and creation date and auditor on the target object in case it implements {@link Auditable} on
* update events.
*
* @param target
*/
@PreUpdate
public void touchForUpdate(Object target) {
Assert.notNull(target, "Entity must not be null!");
if (handler != null) {
AuditingHandler object = handler.getObject();
if (object != null) {
object.markModified(target);
}
}
}
import org.springframework.beans.factory.ObjectFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.auditing.AuditingHandler; import org.springframework.data.domain.Auditable; import org.springframework.stereotype.Component; import org.springframework.util.Assert; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes;
import javax.persistence.PrePersist; import javax.persistence.PreUpdate; import java.util.Optional;
import static com.mega.workflow.suite.common.Constant.HEADER_TENANT_ID; import static com.mega.workflow.suite.common.Constant.WORKSPACE_CODE_HEADER;
@Component public class MegaAuditingEntityListener{ @Autowired private ObjectFactory handler;
}
package com.mega.workflow.suite.common;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; import io.swagger.annotations.ApiModelProperty; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import lombok.experimental.SuperBuilder; import org.hibernate.annotations.Comment; import org.springframework.data.annotation.CreatedBy; import org.springframework.data.annotation.CreatedDate; import org.springframework.data.annotation.LastModifiedBy; import org.springframework.data.annotation.LastModifiedDate;
import javax.persistence.*; import javax.validation.constraints.Min; import java.time.Instant;
@Data @MappedSuperclass @EntityListeners({MegaAuditingEntityListener.class}) @SuperBuilder(toBuilder = true) @AllArgsConstructor @NoArgsConstructor public abstract class BaseEntity {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
// @GenericGenerator(name = "snowFlakeIdGenerator", strategy = "com.mega.workflow.suite.common.SnowFlakeIdGenerator")
// @GeneratedValue(generator = "snowFlakeIdGenerator")
@Comment("主键")
@Column(name = "id", nullable = false)
@Min(value = 1, message = "主键必须大于0")
@ApiModelProperty("主键")
private Long id;
}