简体   繁体   English

即使 JPA 实体不脏,我是否可以强制 spring-data 更新可审计字段?

[英]Can I force spring-data to update auditable fields even if JPA entity not dirty?

I'm using Spring-data-jpa with auditing.我正在使用 Spring-data-jpa 进行审计。 I essentially want to "touch" my entity - I want to update the @LastModifiedDate and @LastModifiedBy fields even though I have made no changes to the object.我本质上想“触摸”我的实体 - 我想更新 @LastModifiedDate 和 @LastModifiedBy 字段,即使我没有对对象进行任何更改。

Essentially, I'm looking to make these two fields work as "LastAccessedBy" and "LastAccessedDate" instead.本质上,我希望将这两个字段改为“LastAccessedBy”和“LastAccessedDate”。

Is there a way to trigger Spring data/JPA to write the entity to the DB (and hence trigger the Spring data auditing module) even if the entity isn't dirty?即使实体不脏,有没有办法触发 Spring 数据/JPA 将实体写入数据库(从而触发 Spring 数据审计模块)?

Or is my only choice to modify an audit field (ex: @LastModifiedDate) and then have spring override it with an updated value?还是我唯一的选择是修改审计字段(例如:@LastModifiedDate),然后让 spring 用更新的值覆盖它?

For what it worth's, I set the lastModifiedDate myself when I want this column to be updated even if I know that my entity is not dirty.对于它的价值,当我希望更新此列时,即使我知道我的实体不脏,我也会自己设置lastModifiedDate

entity.setLastModifiedDate(Instant.now());

The value will be overridden by the @LastModifiedDate annotation, but I least I'll have what I want.该值将被@LastModifiedDate注释覆盖,但至少我会得到我想要的。

My case is : I have a table to track files on my file system, with some functional rules to handle, and if the user upload a file having the same name that the previous one, if I don't set manually the lastModifiedDate I could not tell what the actually last upload date was.我的情况是:我有一个表来跟踪我的文件系统上的文件,有一些功能规则要处理,如果用户上传一个与前一个同名的文件,如果我没有手动设置lastModifiedDate我可以不告诉实际上次上传日期是什么。

(I know I should use the new md5 file to set my entity dirty but this will afraid my manager too much) (我知道我应该使用新的 md5 文件来设置我的实体脏,但这会让我的经理害怕太多)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM