3

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?

Eric B.
  • 23,425
  • 50
  • 169
  • 316
  • Won't getting the row update those values? – We are Borg Sep 30 '15 at 15:42
  • @WeareBorg It shouldn't; if it does, I would see that as a bug. The audit fields should only updated as the object is being persisted. And if the object isn't persisted (because it isn't dirty), the fields won't be updated. – Eric B. Sep 30 '15 at 16:03
  • Oh, i thought u just want to update the fields without that problem, well.. Either direct update of the fields or a trigger after getting the object is what I can think of. I hope some users might know better. Good luck. – We are Borg Sep 30 '15 at 16:31
  • The fields and annotations serve a different purpose, don't abuse it for something else as that will only be confusing. If you want `LastAccessedBy` etc. then create annotations/fields for that case but don't abuse something that isn't meant for it. – M. Deinum Sep 30 '15 at 17:56
  • The problem with changing behavior is that you first would need to modifiy Spring-Data sources. Then, and this is a major issue to me, every other developer looking at your code seeing this annotations expects a certain behavior which doesnt exist anymore. So Martin is right... :-) – Logemann Oct 03 '15 at 09:56
  • @M.Deinum Fair enough. Was trying to be a little lazy, I guess. :) – Eric B. Oct 03 '15 at 15:31
  • @Marc - Actuallly no. I wasn't looking for a way to modify Spring-Data. But rather for a way to force spring-data to merge an entity even if it wasn't dirty. Or, in other words, a way to mark the entity dirty without actually changing any data so that spring-data would pick it up. – Eric B. Oct 03 '15 at 15:36
  • @EricB. Did you solve this problem? – Bhushan Jun 08 '21 at 17:53
  • @Bhushan No; if memory serves, I was never able to get around it, and had to change the approach to manage the "lastAccessed" fields I was trying to generate. – Eric B. Jun 08 '21 at 17:55

2 Answers2

0

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)

stephane brun
  • 234
  • 1
  • 10
0

Try using Spring AuditingHandler https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/auditing/AuditingHandler.html.

@Autowired
private AuditingHandler auditingHandler;
...
auditingHandler.markModified(entity);

And in your unit tests you initialize the AuditingHandler with the following line of code:

AuditingHandler auditingHandler = new AuditingHandler(PersistentEntities.of());
Jean Marois
  • 1,510
  • 11
  • 19