Search code examples
javacouchbasespring-data-couchbasecouchbase-java-api

Compare and Swap : How to map the cas metadata field to document entity of my couchbase


I am using spring data to fetch documents from couchbase and i need to map the check and set metadata field cas to my document in order to implement the optimistic lock.

how can i do it without using n1ql query?

CAS reference in Couchbase

Thanks


Solution

  • I'm not a Java developer, but glancing at the Spring.io docs for Spring Data Couchbase:

    If you want automatic optimistic locking support, all you need to do is add a @Version annotation on a long field like this:

    Example 15. A Document with optimistic locking.

    @Document
    public class User {
    
            @Version
            private long version;
    
            // constructor, getters, setters...
    }
    

    If you load a document through the template or repository, the version field will be automatically populated with the current CAS value.