SpringData MongoDB 在审计期间无法确定 IsNewStrategy [英] SpringData MongoDB cannot determine IsNewStrategy during Auditing
问题描述
我正在尝试使用注释启用审计.我的域类具有在构造对象时填充的 @Id 字段.我为 lastModified 添加了一个 java.util.Date 字段,并用 @LastModifiedDate 对其进行了注释.
I am trying to enable Auditing using Annotations. My domain class has @Id field that is populated while constructing the object. I have added a java.util.Date field for lastModified and annotated it with @LastModifiedDate.
@Document
public class Book {
@Id
private String name;
private String isbn;
@LastModifiedDate
private Date lastModified;
public Book(String name) {
this.name = name;
}
}
我使用
当我尝试保存对象的实例时,出现以下错误:
When I try to save an instance of my object, I get the following error:
Book book1 = new Book("ABCD");
mongoOps.save(book1);
java.lang.IllegalArgumentException:不支持实体 com.pankaj.Book!无法确定 IsNewStrategy.
java.lang.IllegalArgumentException: Unsupported entity com.pankaj.Book! Could not determine IsNewStrategy.
我不想使用 Auditable 接口,也不想从 AbstractAuditable 扩展我的域类.我只想使用注释.因为我对@CreatedBy 和@LastModifiedBy 不感兴趣,所以我也没有实现AuditAware 接口.
I do not want to use the Auditable interface nor extend my domain classes from AbstractAuditable. I only want to use the Annotations. Since I am not interested in the @CreatedBy and the @LastModifiedBy, I am not implementing the AuditAware interface as well.
我只希望 @LastModifiedDate 为我的域类工作.我错过了什么?
I just want the @LastModifiedDate to work for my domain classes. What am I missing?
我使用的是 SpringData MongoDB 1.7.0 版.
I am using version 1.7.0 of SpringData MongoDB.
推荐答案
你没有提到你是如何配置你的 MongoDB 连接的,但是如果你使用的是 AbstractMongoConfiguration,它将使用实际配置类的包来寻找@在启动时记录带注释的类.
You don't mention how you are configuring your MongoDB connection but if you are using AbstractMongoConfiguration, it will use the package of the actual configuration class to look for @Document annotated classes at startup.
如果您的实体在不同的包中,您将必须通过覆盖 AbstractMongoConfiguration.getMappingBasePackage() 手动处理该包.将它放在您的 Mongo Configuration 类中应该可以解决问题(同样,这是考虑到您正在为您的 Mongo 配置扩展 AbstractMongoConfiguration):
If your entities are in a different package, you will have to manually hand that package by overriding AbstractMongoConfiguration.getMappingBasePackage(). Placing this in you Mongo Configuration class should do the trick (again, this is considering you are extending AbstractMongoConfiguration for your Mongo configuration):
@Override
protected String getMappingBasePackage() {
return "package.with.my.domain.classes";
}
这篇关于SpringData MongoDB 在审计期间无法确定 IsNewStrategy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!