问题描述
我目前正在使用Spring数据mongodb 1.6.0-RELEASE,我知道它具有审核功能。我
在我的配置类之上放置 @EnableMongoAuditing
注释。我的bean在下面:
i'm currently using Spring data mongodb 1.6.0-RELEASE and i know it has auditing feature. Iput @EnableMongoAuditing
annotation on top of my configuration class. And my bean is below:
@Document
public class MyBean{
@Id
private AnotherCustomBean anotherCustomBean = new AnotherCustomBean();
@CreatedDate
private Date creationDate;
@LastModifiedDate
private Date lastModifiedDate;
.
.
.
当我用 mongoTemplate.save(myBean)保存这个bean时,
它没有设置创建日期和上次修改日期......并且它没有错误。
When i save this bean with mongoTemplate.save(myBean);
it's not setting created date and last modified date...And it has no errors.
任何帮助都将不胜感激,
Any help would be appreciated,
谢谢。
推荐答案
实际问题是 @Id
注释。要正确使用spring审计,你必须定义一个ObjectId(对于新保存的对象为null),这就是spring如何决定 @LastModifiedDate
和 @CreatedDate
The actual problem was the @Id
annotation. To use spring auditing properly, you have to define an ObjectId (null for new saved objects), thats how spring decide @LastModifiedDate
and @CreatedDate
之后,我找到了一种方法,可以在 @Id
上使用自定义bean实施可审计<字符串,字符串>
Afterwards, i found a way to make it possible use custom beans on @Id
by implementing Auditable<String,String>
感谢@Felby:
这篇关于Spring数据mongodb审计无法正常工作..(Java配置)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!