Spring Data Couchbase

Spring Data for Couchbase is part of the umbrella Spring Data project which aims to provide a familiar and consistent Spring-based programming model for new datastores while retaining store-specific features and capabilities.

Quick Start
Fork me on GitHub

The Spring Data Couchbase project provides integration with the Couchbase Server database. Key functional areas of Spring Data Couchbase are a POJO centric model for interacting with Couchbase Buckets and easily writing a Repository style data access layer.

Features

  • Spring configuration support using Java based @Configuration classes or an XML namespace for the Couchbase driver.
  • CouchbaseTemplate helper class that increases productivity performing common Couchbase operations. Includes integrated object mapping between documents and POJOs.
  • Exception translation into Spring's portable Data Access Exception hierarchy.
  • Feature Rich Object Mapping integrated with Spring's Conversion Service.
  • Annotation based mapping metadata but extensible to support other metadata formats.
  • Automatic implementation of Repository interfaces including support for custom finder methods (backed by Couchbase Views).
  • JMX administration and monitoring
  • Transparent @Cacheable support to cache any objects you need for high performance access.

Quick Start

Download

The recommended way to get started using spring-data-couchbase in your project is with a dependency management system – the snippet below can be copied and pasted into your build. Need help? See our getting started guides on building with Maven and Gradle.

Configuration

package foo;

import foo;

@Configuration
@EnableCouchbaseRepositories
public class Config extends AbstractCouchbaseConfiguration {

    @Override
    protected List<String> bootstrapHosts() {
        return Arrays.asList("host1", "host2");
    }

    @Override
    protected String getBucketName() {
        return "default";
    }

    @Override
    protected String getBucketPassword() {
        return "";
    }
}

Sample Repository

public interface UserRepository extends CrudRepository<User, String> {

    /**
     * Additional custom finder method.
     */
    List<User> findByLastname(Query query);

}

Usage

@Service
public class MyService {

    private final UserRepository userRepository;

    @Autowired
    public MyService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    public void doWork() {
        userRepository.deleteAll();

        User user = new User();
        user.setLastname("Jackson");

        user = userRepository.save(user);

        Query query = new Query();
        query.setKey(ComplexKey.of("Jackson"));
        List<User> allUsers = userRepository.findByLastname(query);

    }
}

Note that to back the custom finders, you need to set up two Views on the server:

For findByLastname:

function (doc, meta) {
  if(doc._class == "com.example.entity.User" && doc.firstname) {
    emit(doc.firstname, null);
  }
}

For findAll and count (also add a reduce _count):

function (doc, meta) {
  if(doc._class == "com.example.entity.User") {
    emit(null, null);
  }
}