How to enable auditing for MongoDB via Annotations in Spring
Asked Answered
L

3

10

I wanted to enable some auditing features, such as @CreatedDate. I am not using Spring xml configuration file, so I cannot add mongo:auditing to Spring configuration. I was wondering if there was an alternative way of enable auditing. The following code is the model for user. But whenever I create a user, the date is not stored in the document, so the auditing it's not working. Could someone give me some help?

@Document(collection = "user")
public class User {
    @Id
    private String id;
    @Indexed(unique = true)
    private String email;
    private String name;
    @CreatedDate
    private Date date;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }
Liechtenstein answered 24/2, 2014 at 14:59 Comment(4)
Please add your configuration to your post. In your code you only have a @CreatedDate annotation not a @CreatedBy so the auditing doesn't know what to insert.Triceratops
@M.Deinum Do I need to set ` @CreatedBy` as well? Is it not possible to just add a ` @CreatedDate` annotation? As I said, I don't use spring config xml in my project. So my question would be: Can I enable auditing in other place than Spring configuration file?Liechtenstein
Currently no, there is no @Enable* annotation in Spring Data MongoDB currently. In the new upcoming release (1.4.0) there is an @EnableMongoAuditing annotation. But currently you are bound to the xml configuration.Triceratops
Just noticed in the GIT commit logs that the new release train for Spring Data is being released. There should be a Spring Data MongoDB 1.4.0.RELEASE out shortly, which contains said annotation.Triceratops
A
17

Because you are not using configuration via XML, I believe you are using annotations. You own a class like this:

public class MongoConfig extends AbstractMongoConfiguration {...}

Thus, in addition to the annotations you should already have, add: @EnableMongoAuditing

Your configuration class will look like this now:

@Configuration
@EnableMongoRepositories(basePackages="...")
@EnableMongoAuditing
public class MongoConfig extends AbstractMongoConfiguration {...}

I hope this helps!

Anachronistic answered 28/1, 2015 at 4:4 Comment(2)
Am using spring boot, and am not defining MongoConfig explicitly. Where to add this annotation thenShrewish
in any @Configuration annotated class, or even in the main class that runs your applicationsFlores
S
4

That's all you need. No subclasses or other stuff.

@Configuration
@EnableMongoAuditing
public class AnyConfig {}
Streeter answered 13/9, 2017 at 15:48 Comment(0)
C
0

You should write a configuration class in which you can connect to MongoDB database using mongoClient by passing db url. and add the anootaion of @EnableMongoAuditing on top of that class.

Chandos answered 19/1, 2017 at 8:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.