在mongo集合上使用时,Spring数据版本注释不会递增 [英] Spring data version annotation not incrementing when used on a mongo collection

查看:194
本文介绍了在mongo集合上使用时,Spring数据版本注释不会递增的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有mongodb的spring数据来存储二进制数据,例如图像等。
我想维护一个版本字段,以附加到网址以欺骗浏览器缓存图像。

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;

我还有一个包装MongoOperations的存储库来保存我的文件。

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);
    }

为了实现版本控制,我做了一些狩猎,发现那里是一个@Version对spring mongo的注释,但是已被弃用。然后我发现应该使用spring数据@Version注释。所以我继续使用弹簧数据@Version注释。

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?

推荐答案

为了使用@Version注释,您需要在应用程序中添加一行来启用Spring Data MongoDB中的审计上下文:

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 />

这篇关于在mongo集合上使用时,Spring数据版本注释不会递增的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆