For the latest stable version, please use Spring Data Couchbase 5.4.0!spring-doc.cn

Migrating from Spring Data Couchbase 3.x to 4.x

This chapter is a quick reference of what major changes have been introduced in 4.x and gives a high-level overview of things to consider when migrating.spring-doc.cn

Please note that implicitly the minimum Couchbase Server version has been bumped up to 5.5 and later, and we recommend running at least 6.0.x.spring-doc.cn

Configuration

Since the main objective was to migrate from the Java SDK 2 to 3, configuration has changed to adapt to the new SDK and also in the long run to prepare it for scopes and collections (but it can still be used without collection support).spring-doc.cn

XML Configuration support has been dropped, so only java/annotation based configuration is supported.

Your configuration still has to extend the AbstractCouchbaseConfiguration, but since RBAC (role-based access control) is now mandatory, different properties need to be overridden in order to be configured: getConnectionString, getUserName, getPassword and getBucketName.If you want to use a non-default scope optionally you can override the getScopeName method.Note that if you want to use certificate based authentication or you need to customize the password authentication, the authenticator method can be overridden to perform this task.spring-doc.cn

The new SDK still has an environment that is used to configure it, so you can override the configureEnvironment method and supply custom configuration if needed.spring-doc.cn

For more information, see Installation & Configuration.spring-doc.cn

Spring Boot Version Compatibility

Spring Boot 2.3.x or higher depends on Spring Data Couchbase 4.x.Earlier versions of Couchbase are not available because SDK 2 and 3 cannot live on the same classpath.spring-doc.cn

Entities

How to deal with entities has not changed, although since the SDK now does not ship annotations anymore only Spring-Data related annotations are supported.spring-doc.cn

Specifically:spring-doc.cn

  • com.couchbase.client.java.repository.annotation.Id became import org.springframework.data.annotation.Idspring-doc.cn

  • com.couchbase.client.java.repository.annotation.Field became import org.springframework.data.couchbase.core.mapping.Fieldspring-doc.cn

The org.springframework.data.couchbase.core.mapping.Document annotation stayed the same.spring-doc.cn

For more information, see Modeling Entities.spring-doc.cn

Automatic Index Management

Automatic Index Management has been redesigned to allow more flexible indexing. New annotations have been introduced and old ones like @ViewIndexed, @N1qlSecondaryIndexed and @N1qlPrimaryIndexed were removed.spring-doc.cn

For more information, see Automatic Index Management.spring-doc.cn

Template and ReactiveTemplate

Since the Couchbase SDK 3 removes support for RxJava and instead adds support for Reactor, both the couchbaseTemplate as well as the reactiveCouchbaseTemplate can be directly accessed from the AbstractCouchbaseConfiguration.spring-doc.cn

The template has been completely overhauled so that it now uses a fluent API to configure instead of many method overloads.This has the advantage that in the future we are able to extend the functionality without having to introduce more and more overloads that make it complicated to navigate.spring-doc.cn

The following table describes the method names in 3.x and compares them to their 4.x equivalents:spring-doc.cn

Table 1. Template Method Comparison
SDC 3.x SDC 4.x

savespring-doc.cn

upsertByIdspring-doc.cn

insertspring-doc.cn

insertByIdspring-doc.cn

updatespring-doc.cn

replaceByIdspring-doc.cn

findByIdspring-doc.cn

findByIdspring-doc.cn

findByViewspring-doc.cn

(removed)spring-doc.cn

findBySpatialViewspring-doc.cn

(removed)spring-doc.cn

findByN1QLspring-doc.cn

findByQueryspring-doc.cn

findByN1QLProjectionspring-doc.cn

findByQueryspring-doc.cn

queryN1QLspring-doc.cn

(call SDK directly)spring-doc.cn

existsspring-doc.cn

existsByIdspring-doc.cn

removespring-doc.cn

removeByIdspring-doc.cn

executespring-doc.cn

(call SDK directly)spring-doc.cn

In addition, the following methods have been added which were not available in 3.x:spring-doc.cn

Table 2. Template Additions in 4.x
Name Description

removeByQueryspring-doc.cn

Allows to remove entities through a N1QL queryspring-doc.cn

findByAnalyticsspring-doc.cn

Performs a find through the analytics servicespring-doc.cn

findFromReplicasByIdspring-doc.cn

Like findById, but takes replicas into accountspring-doc.cn

We tried to unify and align the APIs more closely to the underlying SDK semantics so they are easier to correlate and navigate.spring-doc.cn

For more information, see Template & direct operations.spring-doc.cn

Repositories & Queries

  • org.springframework.data.couchbase.core.query.Query became org.springframework.data.couchbase.repository.Queryspring-doc.cn

  • org.springframework.data.couchbase.repository.ReactiveCouchbaseSortingRepository has been removed.Consider extending ReactiveSortingRepository or ReactiveCouchbaseRepositoryspring-doc.cn

  • org.springframework.data.couchbase.repository.CouchbasePagingAndSortingRepository has been removed.Consider extending PagingAndSortingRepository or CouchbaseRepositoryspring-doc.cn

Support for views has been removed and N1QL queries are now the first-class citizens for all custom repository methods as well as the built-in ones by default.

The behavior itself has not changed over the previous version on how the query derivation is supposed to work. Should you encounter any queries that worked in the past and now do not work anymore please let us know.spring-doc.cn

It is possible to override the default scan consistency for N1QL queries through the new ScanConsistency annotation.spring-doc.cn

The method getCouchbaseOperations() has also been removed. You can still access all methods from the native Java SDK via the class CouchbaseTemplate or Cluster:spring-doc.cn

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.couchbase.core.CouchbaseTemplate;
import org.springframework.stereotype.Service;
import com.couchbase.client.java.Cluster;

@Service
public class MyService {

    @Autowired
    private CouchbaseTemplate couchbaseTemplate;

    @Autowired
    private Cluster cluster;
}

See Couchbase repositories for more information.spring-doc.cn

Full Text Search (FTS)

The FTS API has been simplified and now can be accessed via the Cluster class:spring-doc.cn

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.couchbase.client.java.Cluster;
import com.couchbase.client.java.search.result.SearchResult;
import com.couchbase.client.java.search.result.SearchRow;
import com.couchbase.client.core.error.CouchbaseException;

@Service
public class MyService {

    @Autowired
    private Cluster cluster;

    public void myMethod() {
        try {
          final SearchResult result = cluster
            .searchQuery("index", SearchQuery.queryString("query"));

          for (SearchRow row : result.rows()) {
            System.out.println("Found row: " + row);
          }

          System.out.println("Reported total rows: "
            + result.metaData().metrics().totalRows());
        } catch (CouchbaseException ex) {
          ex.printStackTrace();
        }
    }
}

See the FTS Documentation for more information.spring-doc.cn