春季数据 MongoDB 注释@CreatedDate不起作用,当手动分配 ID 时

我正在尝试使用审核来保存和保存我的对象,但由于我手动设置,因此需要做一些额外的工作。dateCreateddateUpdatedID

根据Oliver Gierke在DATAMONGO-946中的建议,我试图弄清楚如何正确实现它。

作为上面Jira任务的原始海报,我从这里下载了示例 https://github.com/spring-guides/gs-accessing-data-mongodb.git 并对其进行了一些修改:

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);
    }
}

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();
    }
}

执行的结果是这样的:

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在对象更新后变为。null

我在这里错过了什么?如何正确实施,使审计工作正常?Persistable


答案 1

您的代码按预期工作。实现后,您可以看到注释正在工作。Persistable@CreatedDate

确定这是在第二次调用 时,因为该对象已存在于数据库中,并且您使用 更新了它。从以下文档可以看出:createdDatenullsavecreatedDate = null@CreatedDate

@CreatedDate注释。这将标识在实体首次保存到数据库时设置其值的字段。

因此,为了不在第二次调用时覆盖您的,您应该从数据库中检索您的客户,然后对其进行更新。createdDatenullc = repository.findOne("test_id");


答案 2

添加到弹簧引导应用程序中的 main 方法。@EnableMongoAuditing