Spring Mongo Auditing not working @CreatedDate @CreatedBy

Vikram Singh Shekhawat :

I am using Spring Mongo Auditing and @CreatedDate @CreatedBy not working but @LastModifiedDate and @LastModifiedBy working fine.

I added @EnableMongoAuditing on a configuration class and also defined the AuditAware.

@Component("securityAuditorAware")
public class SecurityAuditorAware implements AuditorAware<String> {

 @Override
 public Optional<String> getCurrentAuditor() {
  return Optional.ofNullable(SecurityUtils.getUserPrincipal()).map(AuthenticatedUser::getIssuer);
 } 
}

Auditing class is :

@Document
public class Template  {

 @Id
 private UUID id = UUID.randomUUID();

 @CreatedDate
 private Date createdOn;
 @LastModifiedDate
 private Date modifiedOn;
 @CreatedBy
 private String createdBy;
 @LastModifiedBy
 private String modifiedBy;
}

When I save the document it put null in both createdOn and createdBy but the right values in both modifiedOn and modifiedBy

Thanks for the help

Cường Lư Quốc :

If your Entity does not inheritance Persistable interface, you should define a field which marked by @Version (org.springframework.data.annotation.Version).

@Document
public class Template {

 @Id
 private UUID id = UUID.randomUUID();

 @CreatedDate
 private Date createdOn;
 @LastModifiedDate
 private Date modifiedOn;
 @CreatedBy
 private String createdBy;
 @LastModifiedBy
 private String modifiedBy;
 @Version
 private Integer version;
}

In other side, if your entity inheritance Persistable interface, you should implement logical which to detect the entity never been persisted before.

For more information, you can check two class: PersistentEntityIsNewStrategy and PersistableIsNewStrategy

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

mongodb auditing in spring boot for saving createdDate, lastModifiedDate, createdBy, lastModifiedBy

@EnableMongoAuditing and @CreatedDate Auditing not working in Spring Boot 2.4.3

Spring Data Couchbase @CreatedDate not working

Spring boot - Entity : How to add CreatedDate (UTC timezone aware), LastModifiedDate (UTC timezone aware), CreatedBy, LastModifiedBy

Simple Spring @CreatedDate annotation is not working for me

Spring data mongodb auditing not working.. (Java config)

How to use Mongo Auditing and a UUID as id with Spring Boot 2.2.x?

Spring Data MongoDB <mongo:auditing/> cause IllegalArgumentException: Unsupported entity Could not determine IsNewStrategy

@CreatedBy and @LastModifiedDate are no longer working with ZonedDateTime?

Field based auditing with Spring

Spring Data MongoDB Annotation @CreatedDate isn't working, when ID is assigned manually

Spring Data fills @LastModifiedDate by not @CreatedDate

Spring Data JPA Auditing not working for the JpaRepository update method with @Modifying annotation, why?

Auditing with spring-data-mongodb

Spring Boot and JOOQ Auditing Solutions

Spring JPA Auditing not updating rows

Auditing and @Embedded in Spring Data JPA

Spring Data, Mongo, and @TypeAlias: reading not working

Spring Data Mongo - Custom AggregtionOption not working

How does @CreatedBy work in Spring Data JPA?

Spring REST - @createdDate in Abstract superclass throws error

Auditing tool is not working in Kernel version 3.12.14

Annotation based Auditing for spring-data-elasticsearch?

Disable spring boot auditing in a jhipster generated application

Laravel Auditing package working only on User model and not working on other models

MongoTemplate Pull query not working in mongo shell and spring mongoTemplate

Spring JPA: Query Annotation Fields select not working for Mongo Query

Spring Data Mongo @CreatedAt annotation not working with nested document field

@Indexed on nested property not working in Spring-data for mongo