在特定情况下防止AuditingEntityListener的默认行为

时间:2022-04-28 00:09:20

I use AuditingEntityListener and annotations @CreatedDate and @LastModifiedDate to manage creation/modification dates:

我使用AuditingEntityListener和注释@CreatedDate和@LastModifiedDate来管理创建/修改日期:

//...
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
//...

@Column(name = "creation_time", nullable = false)
@CreatedDate
private LocalDateTime creationDate;

@Column(name = "modification_time", nullable = false)
@LastModifiedDate
private LocalDateTime modificationDate;

However, sometimes I want to be able to modify this entity without creationDate and modificationDate fields being modified as well. Is there a way to implement this?

但是,有时我希望能够在不修改creationDate和modificationDate字段的情况下修改此实体。有没有办法实现这个?

1 个解决方案

#1


1  

Just use correspond column properties:

只需使用对应的列属性:

@Column(name = "creation_time", nullable = false, updatable = false)
@CreatedDate
private LocalDateTime creationDate;

@Column(name = "modification_time", nullable = false, insertable = false)
@LastModifiedDate
private LocalDateTime modificationDate;

I think your issue was not with AuditingEntityListener.

我认为您的问题不在于AuditingEntityListener。


If you interesting manage in your own way modificationDate you should not use @LastModifiedDate annotation near modificationDate field and implement your special rules for simple field:

如果您有兴趣以自己的方式管理modifyDate,则不应在modificationDate字段附近使用@LastModifiedDate注释,并为简单字段实现特殊规则:

@Column(name = "modification_time", nullable = false)
private LocalDateTime modificationDate;

#1


1  

Just use correspond column properties:

只需使用对应的列属性:

@Column(name = "creation_time", nullable = false, updatable = false)
@CreatedDate
private LocalDateTime creationDate;

@Column(name = "modification_time", nullable = false, insertable = false)
@LastModifiedDate
private LocalDateTime modificationDate;

I think your issue was not with AuditingEntityListener.

我认为您的问题不在于AuditingEntityListener。


If you interesting manage in your own way modificationDate you should not use @LastModifiedDate annotation near modificationDate field and implement your special rules for simple field:

如果您有兴趣以自己的方式管理modifyDate,则不应在modificationDate字段附近使用@LastModifiedDate注释,并为简单字段实现特殊规则:

@Column(name = "modification_time", nullable = false)
private LocalDateTime modificationDate;