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?
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 />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With