Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpringData MongoDB cannot determine IsNewStrategy during Auditing

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.

like image 634
user3458433 Avatar asked Dec 20 '22 02:12

user3458433


2 Answers

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";
}
like image 64
LuizPoleto Avatar answered Dec 28 '22 07:12

LuizPoleto


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")
like image 31
khawarizmi Avatar answered Dec 28 '22 06:12

khawarizmi