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;
}
}
I have enabled auditing in the Spring Configuration XML using <mongo:auditing/>.
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: Unsupported entity com.pankaj.Book! Could not determine IsNewStrategy.
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.
I just want the @LastModifiedDate to work for my domain classes. What am I missing?
I am using version 1.7.0 of SpringData MongoDB.
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.
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";
}
I had same issue, later I determined that I was missing ID field with annotation;
@Id
private String Id
in my class I was trying to persist with
@Document(collection="collectionName")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With