简体   繁体   中英

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

I'm using Spring-data-jpa with auditing. 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.

Essentially, I'm looking to make these two fields work as "LastAccessedBy" and "LastAccessedDate" instead.

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?

Or is my only choice to modify an audit field (ex: @LastModifiedDate) and then have spring override it with an updated value?

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.

entity.setLastModifiedDate(Instant.now());

The value will be overridden by the @LastModifiedDate annotation, but I least I'll have what I want.

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.

(I know I should use the new md5 file to set my entity dirty but this will afraid my manager too much)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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