java mongodb 注解_java – Spring数据MongoDB注释@CreatedDate在手动分配ID时不起作用

我正在尝试使用审计来在我的对象中保存dateCreated和dateUpdated,但由于我手动设置了ID,所以还有一些额外的工作.

遵循Oliver Gierke在DATAMONGO-946的建议

我想弄清楚如何正确实现它.

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 {

@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使审计工作正常进行?

解决方法:

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

当第二次调用save时,createdDate为null,因为该对象已存在于数据库中,并且您使用createdDate = null更新了它.正如您从@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”)从数据库中检索您的客户;然后更新它.

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

来源: https://codeday.me/bug/20191006/1858220.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值