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

编程入门 行业动态 更新时间:2024-10-27 12:25:45
本文介绍了当手动分配ID时,Spring Data MongoDB Annotation @CreatedDate不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用审核在我的对象中保存 dateCreated 和 dateUpdated ,但是因为我设置了 ID 手动,还有一些额外的工作。

I'm trying to use auditing to save dateCreated and dateUpdated in my objects, but since I set ID manually, there's some additional work.

遵循Oliver Gierke在 DATAMONGO-946 我正在试图弄清楚如何正确实现它。

Following Oliver Gierke's suggestion in DATAMONGO-946 I'm trying to figure out how to correctly implement it.

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

As original poster in Jira task above, I've downloaded example from here github/spring-guides/gs-accessing-data-mongodb.git and modified it a bit:

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

What am I missing here? And how to correctly implement Persistable to make auditing work properly?

推荐答案

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

Your code is working as expected. After you've implemented Persistable you can see that @CreatedDate annotation is working.

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

Sure that createdDate is null on the second call of save because the object already exists in the database and you updated it with createdDate = null. As you can see from the documentation for @CreatedDate:

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

@CreatedDate annotation. This identifies the field whose value is set when the entity is persisted to the database for the first time.

所以不是要在第二次调用时用 null 覆盖 createdDate ,您应该使用 c = repository.findOne(test_id); 然后更新它。

So not to overwrite your createdDate with null on the second call you should retrieve your customer from the database with c = repository.findOne("test_id"); and then update it.

更多推荐

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

本文发布于:2023-11-22 17:23:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618340.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   分配   Spring   ID   Data

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!