8

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;
    }
Julio
  • 849
  • 2
  • 10
  • 17
  • 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. – M. Deinum Feb 24 '14 at 15:14
  • @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? – Julio Feb 24 '14 at 15:50
  • 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. – M. Deinum Feb 24 '14 at 19:34
  • 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. – M. Deinum Feb 24 '14 at 19:35

3 Answers3

15

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!

araujodavid
  • 194
  • 2
  • 4
  • Am using spring boot, and am not defining MongoConfig explicitly. Where to add this annotation then – Anunay Jun 10 '16 at 08:44
  • in any @Configuration annotated class, or even in the main class that runs your applications – Rohi Jan 31 '17 at 21:05
2

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

@Configuration
@EnableMongoAuditing
public class AnyConfig {}
Drew Wills
  • 8,408
  • 4
  • 29
  • 40
Pavel Vlasov
  • 4,206
  • 6
  • 41
  • 54
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.

Mehrish Yousuf
  • 195
  • 1
  • 3
  • 12