spring-projects / spring-data-commons

Spring Data Commons. Interfaces and code shared between the various datastore specific implementations.
https://spring.io/projects/spring-data
Apache License 2.0
777 stars 675 forks source link

AuditorAware#getCurrentAuditor called for Entities only marked CreatedAt [DATACMNS-1404] #1839

Open spring-projects-issues opened 6 years ago

spring-projects-issues commented 6 years ago

Yuki Yoshida opened DATACMNS-1404 and commented

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.

 


Affects: 2.0.10 (Kay SR10)

Reference URL: https://github.com/sis-yoshiday/spring-data-auditing-issue

spring-projects-issues commented 4 years ago

Mathias Ewald commented

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

spring-projects-issues commented 4 years ago

Oliver Drotbohm commented

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

spring-projects-issues commented 4 years ago

Mathias Ewald commented

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.

 

 

spring-projects-issues commented 3 years ago

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.