Robin Ellerkmann
Robin Ellerkmann

Reputation: 2113

Defining query consistency in Spring Data Couchbase

I try to establish a deleteAll function which deletes all documents which are associated with a given repository and class. In order to this I created a custom N1ql query. But I want the Couchbase index to be updated before later database operations take place. My guess is that I have to change the consistency level of the query to achieve this behaviour. Here and here I found some examples which do this by using the CouchbaseTemplate. But my template is null. Could anybody tell me what I am doing wrong?

public void deleteAll() throws DBException {
    CouchbaseOperations couchbaseTemplate;
    try {
        couchbaseTemplate = templateProvider.resolve(getRepository().getClass(), getClassName().getClass());
    } catch (Exception e) {
        throw  new DBException("Could not get couchbase client", e);
    }

    String statement = String.format("DELETE FROM %s WHERE _class='%s'",
                                     couchbaseTemplate.getCouchbaseBucket().name(), getClassName());

    ScanConsistency consistency = couchbaseTemplate.getDefaultConsistency().n1qlConsistency();
    N1qlParams queryParams = N1qlParams.build().consistency(consistency);
    N1qlQuery query = N1qlQuery.simple(statement, queryParams);
    N1qlQueryResult result = couchbaseTemplate.queryN1QL(query);

    //Result handling
    }
}

The templateProvider is autowired.

Upvotes: 0

Views: 413

Answers (1)

subhashni
subhashni

Reputation: 216

It is not entirely clear about your repository and entity from your code snippet. Which version of SDC are you using?

If you are using operation mapping bean, you get the underlying couchbase template for the particular repository and entity using

@Repository
public interface MyRepository extends CrudRepository<MyEntity, String> {
}

public class MyService {
@Autowired
MyRepository repo;

@Autowired
RepositoryOperationsMapping templateProvider;
....
CouchbaseOperations operations = templateProvider.resolve(repo.getClass(),MyEntity.class);

Make sure to enable couchbase repositories with @EnableCouchbaseRepositories. If your repositories only use couchbase, you can also get the couchbase template bean directly.

@Autowired
CouchbaseTemplate template;

Upvotes: 1

Related Questions