Announcement Announcement Module
Collapse
No announcement yet.
Spring Data Mongo DB Auditing Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring Data Mongo DB Auditing

    Hello,

    I am currently trying to migrate from Morphia over to Spring Data MongoDB (SDM). I have managed to figure out some things on my own, but I am stuck with the created/last modified dates. I see there is support with auditing but I don't know how to get it to work.

    Here is an example dot:

    Code:
    @Document(collection = "entities")
    public class Entity extends Serializable {
    	private static final long serialVersionUID = 1L;
    
    	@Id
    	protected String id;
    
    	@CreatedDate
    	protected DateTime createdDate;
    
    	@LastModifiedDate
    	protected DateTime lastModifiedDate;
    
    	@Version
    	protected Long version;
    
    	/**
    	 * Returns the id
    	 * 
    	 * @return the id
    	 */
    	public String getId() {
    		return id;
    	}
    
    	/**
    	 * Returns the created date
    	 * 
    	 * @return the created date
    	 */
    	public DateTime getCreatedDate() {
    		return createdDate;
    	}
    
    	/**
    	 * Returns the last modified date
    	 * 
    	 * @return the last modified date
    	 */
    	public DateTime getLastModifiedDate() {
    		return lastModifiedDate;
    	}
    
    	/**
    	 * Returns the version
    	 * 
    	 * @return the version
    	 */
    	public Long getVersion() {
    		return version;
    	}
    }
    Here is the code I am using to configure SDM:

    Code:
    // Create the DB factory
    SimpleMongoDbFactory dbFactory = new SimpleMongoDbFactory(
        // Get the mongoclient instance for the given host and port
        MongoClientManager.getInstance().getClient("localhost",27017),
        "db"
    );
    
    // Create a non default convertor so that we don't store "_class"
    // properties in the database since we don't need that at the moment.
    MappingMongoConverter converter = new MappingMongoConverter(
    	dbFactory,
    	new MongoMappingContext()
    );
    converter.setTypeMapper(new DefaultMongoTypeMapper(null));
    
    // Create our template to the users database
    this.template = new MongoTemplate(dbFactory, converter);
    
    // Create the DAO for the user collection
    RepositoryFactorySupport repositoryFactory = new MongoRepositoryFactory(this.template);
    this.dao = repositoryFactory.getRepository(Entity.class);
    Note that I am doing everything in code and not using any app-context.xml or AppConfig class for the following reasons:
    1. I'm not sure how our current initialization code would work with Springs IoC initialization
    2. It is a utility jar and I don't know how to properly configure that (I've only seen examples of complete self contained apps/webapps)
    3. I'm a noob and still learning

    From what I gather I need to add a <mongo:auditing /> element to the app-context.xml to enable auditing, but as I've said I'm not using an app-context.xml

    Also, I did see the Auditable interface. I was thinking of adding that to my entity to see if that did anything, but it include other things like the created by and last modified by user, which I don't want/need.

  • #2
    What's hidden behind <mongo:auditing /> is equivalent to the following:

    Code:
    MongoMappingContext context = …;
    IsNewStrategyFactory factory = new MappingContextIsNewStrategyFactory(context);
    IsNewAwareAuditingHandler<Object> handler = new IsNewAwareAuditingHandler<Object>(factory);
    AuditingEventListener listener = new AuditingEventListener(handler);
    If you register the listener instance created as Spring bean (e.g. through an @Bean method), it will receive the pre-save etc. events and trigger the auditing transparently. We will have an @EnableMongoAuditing annotation simplifying all of this available in the upcoming milestone of the Babbage release.

    Comment

    Working...
    X