【发布时间】:2020-01-28 16:49:02
【问题描述】:
当我创建需要自动获取的任何用户时,该用户已使用 @CreatedBy 注释创建了用户,并且当我使用注释 @LastModifiedBy 手动操作任何需要自动获取的内容时。但这现在不起作用。可能是什么原因?
This is my entity class
@Slf4j
@Getter
@Setter
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class AbstractAuditingEntity implements Serializable {
private static final long serialVersionUID = 1L;
@CreatedBy
@Column(name = "created_by" , nullable = false, length = 50, updatable = false)
@JsonIgnore
private String createdBy;
@CreatedDate
@Column(name = "created_date", updatable = false)
@JsonIgnore
private Instant createdDate = Instant.now();
@LastModifiedBy
@Column(name = "last_modified_by", length = 50)
@JsonIgnore
private String lastModifiedBy;
@LastModifiedDate
@Column(name = "last_modified_date")
@JsonIgnore
private Instant lastModifiedDate = Instant.now();
@Transient
public <T> T getView(Class<T> viewClass) {
try {
T view = viewClass.getDeclaredConstructor().newInstance();
BeanUtils.copyProperties(this, view);
return view;
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
log.error("Error mapping model to view", e);
}
return null;
}
【问题讨论】:
-
你需要创建一个实现
AuditorAware<String>的自定义类并定义它的getCurrentAuditor() -
添加到@One 的回答。这取决于您从哪里获取用户。校长的例子
标签: hibernate spring-boot jpa spring-data-jpa