Annotation based Auditing for spring-data-elasticsearch?

MitchWebber :

I am trying to use the annotation based approach to audit my elasticsearch document using spring-data-elasticsearch. I followed the JPA guide while implementing my classes/setup.

My code looks like this:

@Configuration
@EnableJpaAuditing
public class SpringSecurityAuditorAwareConfiguration {

    @Bean
    public AuditorAware<String> auditorProvider() {
        return new SpringSecurityAuditorAware();
    }

}

public class SpringSecurityAuditorAware implements AuditorAware<String> {

    public Optional<String> getCurrentAuditor() {

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

@Document(indexName =  "myEntity", type = "myEntityType")
public class MyEntity {

    @Id
    private String id;

    @CreatedBy
    protected String createdBy;

    @CreatedDate
    protected OffsetDateTime createdDate;

    @LastModifiedBy
    protected String lastModifiedBy;

    @LastModifiedDate
    protected OffsetDateTime lastModifiedDate;

    public void setDictionaryId(DictionaryId dictionaryId) {
        this.dictionaryId = dictionaryId;
    }

    public String getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

    public OffsetDateTime getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(OffsetDateTime createdDate) {
        this.createdDate = createdDate;
    }

    public String getLastModifiedBy() {
        return lastModifiedBy;
    }

    public void setLastModifiedBy(String lastModifiedBy) {
        this.lastModifiedBy = lastModifiedBy;
    }

    public OffsetDateTime getLastModifiedDate() {
        return lastModifiedDate;
    }

    public void setLastModifiedDate(OffsetDateTime lastModifiedDate)     {
        this.lastModifiedDate = lastModifiedDate;
    }

}

Unfortunately it when I save a new instance the properties are always set to null. Does spring-data-elasticsearch support the annotation based approach?

P.J.Meisch :

Edit:

This is implemented since version 4.0 which has been released in May 2020.

Original answer:

No, this is currently not supported in Spring Data Elasticsearch.

Please create a ticket to add support for this, this is definitely a feature worth to have.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Elasticsearch Spring Data with RestHighLevelClient

Auditing with spring-data-mongodb

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

annotation based Spring bean validation

How to use Auditing in JPA/Spring-Data JPA?

reference data is lost when form fails validation in spring3 annotation based controller

What's the use of a mapping annotation and do I need it?(Spring Data ElasticSearch)

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

Field based auditing with Spring

How to configure Auditing via Java Config in Spring Data (and Spring Data Rest)?

Auditing and @Embedded in Spring Data JPA

Spring, annotation based, overriding of components

data auditing in Cassandra

Annotation for UDT by Spring data Cassandra

Spring Data Elasticsearch's @Field annotation not working

How to config not_analyzed with @Field annotation in spring-data-elasticsearch 3.0.0.RC2

Spring Data Elasticsearch setting annotation did not take effect

Spring Boot and JOOQ Auditing Solutions

spring 5 MVC Annotation Based Application Not Working

Maven: How to decide Spring Data Elasticsearch pom version based on Spring framework version?

Spring annotation based caching

Spring JPA Auditing not updating rows

Annotation based Spring Activiti ProcessEngine

Spring Data Neo4j and Spring Data Auditing

DataTableRepository in Spring data Elasticsearch

Java annotation based Spring security config

@CreatedDate annotation does not work with spring-data-elasticsearch

FieldNamingStrategy with Spring Data Elasticsearch

Spring Data Elasticsearch Inheritance