当手动分配ID时,Spring Data MongoDB Annotation @CreatedDate不起作用

时间:2016-02-23 17:30:52

标签: java spring mongodb spring-data spring-data-mongodb

我尝试使用审核功能在我的对象中保存dateCreateddateUpdated,但由于我手动设置了ID,因此还有其他一些工作。< / p>

遵循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以使审核工作正常进行?

3 个答案:

答案 0 :(得分:6)

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

createdDate的第二次调用确实nullsave,因为该对象已存在于数据库中,并且您使用createdDate = null更新了该对象。正如您在@CreatedDate的文档中看到的那样:

  

@CreatedDate注释。这标识了其值已设置的字段   当实体第一次持久化到数据库时。

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

答案 1 :(得分:0)

最简单的解决方案是将版本属性(用@Version 注释)添加到您的 Customer 类并保持未初始化。这会将值 0 分配给任何新创建的对象,从而告诉 spring 这是一个新对象。

public/blog/assets/styles/app.css

注意:每次修改此对象时,此版本将自动递增

答案 2 :(得分:0)

@EnableMongoAuditing 添加到 Spring Boot 应用程序的 main 方法中。