Spring数据mongodb审计无法正常工作..(Java配置) [英] Spring data mongodb auditing not working.. (Java config)

查看:92
本文介绍了Spring数据mongodb审计无法正常工作..(Java配置)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用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. I put @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:


我发现@Id字段在save()
时只需要为@CreatedDate和@CreatedBy注释时为空。
@LastModifiedDate和@LastModifiedBy字段无论
是否有效,无论@Id字段是否已初始化。

I found that the @Id field needed to be null at the time of save() only for the @CreatedDate and @CreatedBy annotations. The @LastModifiedDate and @LastModifiedBy fields worked regardless of whether the @Id field was initialized or not.

这篇关于Spring数据mongodb审计无法正常工作..(Java配置)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆