0

I have a simple entity where I want to let "createdDate" assigned automatically.

@CreatedDate
private LocalDateTime createdDate;

As also in documentation described, I have also added the annotation "@EnableCassandraAuditing" in my CassandraConfig:

@Configuration
@EnableCassandraRepositories
@EnableCassandraAuditing
public class CassandraConfig extends AbstractCassandraConfiguration { 

But It still does not work.. The entities are created with createdDate=null. Any help appreciated.

akcasoy
  • 6,497
  • 13
  • 56
  • 100
  • It is missing to configure your `Auditable User` you need to create your own implementation since this does not work for it. – Jonathan JOhx Oct 23 '19 at 18:11

1 Answers1

0

Based the documentation says:

We provide @CreatedBy, @LastModifiedBy to capture the user who created or modified the entity as well as @CreatedDate and @LastModifiedDate to capture the point in time this happened.

I think the last one sentence means capture the point in time this happened by some user. Since I had the same issue a long time ago.

Then if you want to @CreatedDate annotation works then you need to give a current auditor. Should create your custom Auditor Aware by following way or simply the second one example.

First Example:

class SpringSecurityAuditorAware implements AuditorAware<User> {

    public Optional<User> getCurrentAuditor() {

        return Optional.ofNullable(SecurityContextHolder.getContext())
                 .map(SecurityContext::getAuthentication)
                 .filter(Authentication::isAuthenticated)
                 .map(Authentication::getPrincipal)
                 .map(User.class::cast);
    }
}

Second Example:

class SpringSecurityAuditorAware implements AuditorAware<String> {

    public Optional<String> getCurrentAuditor() {
         return Optional.of("CurrentUser");
    }
}

NOTE

I found an answer similar that you can check @CreatedDate does not work answer

Jonathan JOhx
  • 5,784
  • 2
  • 17
  • 33
  • "**In case you use either @CreatedBy or LastModifiedBy, the auditing infrastructure somehow needs to become aware of the current principal.**" You need those just for CreatedBy and LastModifiedBy annotations.. why should you need the user entity at all for "createdDate"? – akcasoy Oct 23 '19 at 18:19
  • Please remove `upvoted` @akcasoy since I had the same issue, when you want to `@CreatedDate` annotation works then you need to give a current auditor. Based the documentation says: `We provide @CreatedBy, @LastModifiedBy to capture the user who created or modified the entity as well as @CreatedDate and @LastModifiedDate to capture the point in time this happened.` I think the last one means capture the point in time this happened by some user. you can check this answer https://stackoverflow.com/a/20508028/10426557 – Jonathan JOhx Oct 23 '19 at 19:13
  • sorry.. I really have tried but it does not work. it also makes no sense.. what if I have no security at all? What if i just want to save some entities from public endpoints where u don't have any user? And as I told before, documentation clearly points out "**In case you use either @CreatedBy or LastModifiedBy, the auditing infrastructure somehow needs to become aware of the current principal**" – akcasoy Oct 24 '19 at 19:16
  • Yes, I agree with that but I think it is more one issue related to how should work the `@CreatedDate` it is weird that doesn't not work. it would be great if you provide a small code example so that I can reproduce and help you as well :) @akcasoy – Jonathan JOhx Oct 24 '19 at 21:02
  • someone tried successfully? do you have an example? – ozzem May 22 '20 at 13:20