Spring data version annotation not incrementing when used on a mongo collection

…衆ロ難τιáo~ 提交于 2019-12-03 12:40:47

问题


I am using spring data with mongodb to store binary data such as images etc I want to maintain a version field to append to the url to cheat the browser from caching images.

See my document base class below:

import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Version;
import org.springframework.data.mongodb.core.index.Indexed;

public abstract class BaseDocument {

    @Id
    @Indexed(unique=true)
    protected long id;
    protected byte[] data;
    protected String mimeType;
    protected String filename;
    protected String extension;
    @Version
    private Long version;

I also have a repository wrapping MongoOperations for saving my documents.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class DocumentRepository implements IDocumentRepository {

    @Autowired
    private MongoOperations mongoTemplate;

    @Override
    public <D extends BaseDocument> void saveDocument(D document) {
        mongoTemplate.save(document);
    }

In an attempt to implement versioning, I did some hunting around and found that there was a @Version annotation for spring mongo but that is was deprecated. I then discovered that the spring data @Version annotation should used instead. So I went ahead and used the spring data @Version annotation.

What I expect to happen is that my version field would be incremented every time I save my document. I am overwriting the same document several times but my version field is not getting incremented as I am expecting.

Am I doing something incorrectly or is there something additional that I need to do?


回答1:


In order to use @Version annotation you need to enable auditing in Spring Data MongoDB by adding one line to your application context:

<mongo:auditing />


来源:https://stackoverflow.com/questions/16665797/spring-data-version-annotation-not-incrementing-when-used-on-a-mongo-collection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!