Printemps de Données MongoDB Annotation @CreatedDate ne fonctionne pas, lorsque l'ID est attribué manuellement

Je suis en train de l'utilisation de l'audit pour enregistrer dateCreated et dateUpdated dans mes objets, mais depuis que j'ai mis ID manuellement, il y a un peu plus de travail.

Suivantes Oliver Gierke de la suggestion dans DATAMONGO-946
Je suis à essayer de comprendre comment appliquer correctement il.

Comme affiche originale dans Jira tâche ci-dessus, j'ai téléchargé exemple à partir d'ici https://github.com/spring-guides/gs-accessing-data-mongodb.git et modifié un peu:

package hello;

import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.domain.Persistable;

import java.util.Date;

public class Customer implements Persistable<String> {
    @Id
    private String id;
    @CreatedDate
    private Date createdDate;
    @LastModifiedDate
    private Date lastModifiedDate;
    private String firstName;
    private String lastName;
    private boolean persisted;

    public Customer() {
    }

    public Customer(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public void setPersisted(boolean persisted) {
        this.persisted = persisted;
    }

    @Override
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Override
    public boolean isNew() {
        return !persisted;
    }

    @Override
    public String toString() {
        return String.format(
                "Customer[id=%s, createdDate=%s, lastModifiedDate=%s, firstName='%s', lastName='%s']",
                id, createdDate, lastModifiedDate, firstName, lastName);
    }
}

et

package hello;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.mongodb.config.EnableMongoAuditing;
@SpringBootApplication
@EnableMongoAuditing
public class Application implements CommandLineRunner {
@Autowired
private CustomerRepository repository;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void run(String... args) throws Exception {
repository.deleteAll();
//create a customer
Customer c = new Customer("Alice", "Smith");
c.setId("test_id");
//save a customer
repository.save(c);
//fetch all customers
System.out.println("Customers found with findAll():");
System.out.println("-------------------------------");
for (Customer customer : repository.findAll()) {
System.out.println(customer);
}
System.out.println();
//create another customer with same id
c = new Customer("Bob", "Smith");
c.setId("test_id");
c.setPersisted(true);
repository.save(c);
//fetch all customers
System.out.println("Customers found with findAll():");
System.out.println("-------------------------------");
for (Customer customer : repository.findAll()) {
System.out.println(customer);
}
System.out.println();
}
}

et un résultat de l'exécution est ceci:

Customers found with findAll():
-------------------------------
Customer[id=test_id, createdDate=Wed Feb 24 00:43:47 WITA 2016, lastModifiedDate=Wed Feb 24 00:43:47 WITA 2016, firstName='Alice', lastName='Smith']
Customers found with findAll():
-------------------------------
Customer[id=test_id, createdDate=null, lastModifiedDate=Wed Feb 24 00:43:47 WITA 2016, firstName='Bob', lastName='Smith']

createdDate devient null après l'objet de mise à jour.

Ce qui me manque ici? Et comment implémenter correctement Persistable de faire des travaux d'audit correctement?

InformationsquelleAutor and_rew | 2016-02-23