[DATACMNS-1404] AuditorAware#getCurrentAuditor called for Entities only marked CreatedAt Created: 12/Oct/18  Updated: 09/Apr/20

Status: Waiting for Feedback
Project: Spring Data Commons
Component/s: Core
Affects Version/s: 2.0.10 (Kay SR10)
Fix Version/s: None

Type: Improvement Priority: Minor
Reporter: Yuki Yoshida Assignee: Oliver Drotbohm
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

spring-data-commons 2.0.10


Reference URL: https://github.com/sis-yoshiday/spring-data-auditing-issue
Last updater: Mathias Ewald

 Description   

When we have below two entities.

 

@Entity
@EntityListeners(AuditingEntityListener.class)
public class EntityWithCreatedBy { 
  @Id @GeneratedValue private Integer id;

  @ManyToOne(fetch = FetchType.LAZY)
  @CreatedBy 
  private User createdBy;
}

 

@Entity
@EntityListeners(AuditingEntityListener.class)
public class EntityWithOnlyCreatedAt {

  @Id
  @GeneratedValue
  private Integer id;

  @CreatedDate
  private LocalDateTime createdDate;
}

And try to save them, then `org.springframework.data.domain.AuditorAware#getCurrentAuditor` was called for both of them.

 

It would be nice when we save entity that does not have `@CreatedBy` or `@LastModifiedBy`,

`AuditorAware#getCurrentAuditor` stop being called.

 

I attached reproduction project (by spring-data-jpa) to Reference URL.

 



 Comments   
Comment by Mathias Ewald [ 09/Apr/20 ]

I am currently looking at the same problem. Has anyone found a workaround to this?

Comment by Oliver Drotbohm [ 09/Apr/20 ]

Looks like this could be an easy fix. Can you elaborate what the issue with that (admittedly superfluous) call is?

Comment by Mathias Ewald [ 09/Apr/20 ]

Disclaimer: What I am doing there might be very close to insanity. I am merely a hobby developer 

In my specific situation, I overwrite the findById method of a JPA repository that serves the same type as AuditorAware provides in order to add some synchronization logic. Somewhere down the line, I call EntityManager#persist (as I said - synchronization stuff) which triggers the implementation of AuditorAware. That in turn calls findById and here comes the stackoverflow error.

I got around that for now by getting rid of JPA auditing and using @PrePersist and @PreUpdate to set the creation and modification timestamps.

 

 

Generated at Sat Jul 04 19:59:39 UTC 2020 using Jira 8.5.4#805004-sha1:0444eab799707f9ad7b248d69f858774aadfd250.