CouchbaseTemplate is the central class of Spring Data Couchbase for working with a Couchbase bucket from Java. It maps your entities to JSON documents and offers a fluent API for inserting, upserting, replacing, finding, querying and removing them. Repositories are built on top of it, and you inject it directly when you need finer control than a repository method gives you.
In Spring Boot, adding spring-boot-starter-data-couchbase and the connection properties is enough for CouchbaseTemplate to be auto-configured as a bean.
CouchbaseTemplate implements CouchbaseOperations and sits between your domain objects and the Couchbase Java SDK. Its responsibilities are:
@Document, @Id, @Field).DataAccessException hierarchy.For plain CRUD, CouchbaseRepository is simpler. Choose the template for dynamic queries, partial updates, custom scopes or collections, or operations that repositories do not express.
Add the starter and configure the connection in application.properties:
Define the entity. Each document needs an id:
The template methods follow a fluent pattern: choose the operation and type, then call one(...) or all(...).
The differences matter. insertById fails with a duplicate key exception if the id already exists. replaceById fails if it does not exist. upsertById creates or overwrites. Key-value operations by id are the fastest way to read and write in Couchbase because they do not use the query service.
For queries, build a Query with QueryCriteria, so there is no hand-written SQL string:
The template generates a N1QL statement, adds the type filter for the entity, and maps each row back to a Product. Other fluent methods cover counts and removal by query (count(), all() on removeByQuery). The older findByN1QL method from early tutorials does not exist in the current API; use findByQuery.
Queries need an index on the fields you filter by. With auto-index-creation=true, indexes declared through annotations such as @QueryIndexed are created at startup, which is convenient in development but is better managed explicitly in production.
Couchbase organises data into bucket, scope and collection. The fluent API lets you override the target per call:
| Need | Use |
|---|---|
Simple CRUD and derived finders such as findByName | CouchbaseRepository |
| Dynamic or conditional queries built at runtime | CouchbaseTemplate with Query |
| Different scope or collection per call | CouchbaseTemplate |
| Fine control over insert, replace and upsert semantics | CouchbaseTemplate |
Yes. With the starter and a connection string, credentials and bucket name, Boot creates the couchbaseTemplate bean, and you only inject it.
insertById fails if the document id exists. upsertById writes the document whether or not it exists.
Yes. ReactiveCouchbaseTemplate offers the same fluent API returning Mono and Flux, and it is available through the reactive starter setup.
Usually there is no suitable index for the filtered field. Create a secondary index and check the query plan with EXPLAIN.
CouchbaseTemplate is the lower-level, flexible way to access Couchbase from Spring. Use insertById, upsertById, replaceById, findById and removeById for key-value access, findByQuery with QueryCriteria for N1QL queries, and the scope and collection methods for multi-collection layouts. Keep repositories for simple CRUD and the template for everything else.