Spring Data Couchbase Example

In this example we shall demonstrate how we can connect Spring Data with Couchbase.

1. What is CouchBase?

Couchbase is a highly scalable, Document based NoSQL Database. Document based NoSQL databases work on map-like concept of KEY-VALUE pairs. The key being uniquely identifiable property like a String, path etc and the value being the Document that is to be saved. Another example of Document based NoSQL is MongoDB. In one of our previous examples, we have already demonstrated how we can connect and manage Spring Data with MongoDB.

Spring Data CouchBase is the Spring module which helps us in integrating with the CouchBase Database Server.As with the other modules demonstrated in this series, this module too provides supports both for derived queries(based on the method names) and the annotated query.

2. Project Set-Up

So, let’s start the project setup by installing CouchBase Database Server from here. We have used the Community Edition for this example.

Upon successful installation the user will be directed to this page : http://localhost:8091/index.html

Here is how the page looks like:

Fig 1 : Successful Couchbase Setup
Fig 1 : Successful Couchbase Setup

Next step is to create a new Data-Bucket. Data-Bucket is like analogous to a Table in RDBMS or to a Collection in a MongoDb Database. This can done by clicking on the Create New Data Bucket button present on the Data Buckets tab. We are naming it JavaCodeGeeks for the sake of this example. We shall be adding our documents to this data bucket.

Now that the CouchBase server is up and running, we will setup the application environment.
Create a simple Maven Project in Eclipse IDE by selecting the Skip Archetype Selection checkbox from the New Maven Project Pop-up. We are using the below pom.xml to manage the dependencies for CouchBase from Spring Data.


<project xmlns="" xmlns:xsi=""




Eclipse will download the required JAR files and add the dependencies in the project classpath. Now that the project is setup and dependencies imported, we can begin writing the actual code.

3. Implementation

The implementation will consist of three major configuration files. The first one is the actual domain object which will be persisted in the CouchBase Database as the Document.

package com.jcg.examples.entity;



public class Book

		private long bookId;
		private String name;
		private long isbnNumber;

		public long getBookId()
				return bookId;

		public void setBookId(long bookId)
				this.bookId = bookId;

		public String getName()
				return name;

		public void setName(String name)
		{ = name;

		public long getIsbnNumber()
				return isbnNumber;

		public void setIsbnNumber(long isbnNumber)
				this.isbnNumber = isbnNumber;

		public String toString()
				return "Book [bookId=" + bookId + ", name=" + name + ", isbnNumber=" + isbnNumber + "]";

@Document annotation is used to mark the PoJo as the Couchbase Document. It has an expiry attribute which is the TTL of the document.

@Id marks the corresponding instance variable as document Id in the database.We shall retrieve the document later based on this Id.

Next is the basic Repository Class:

package com.jcg.examples.repo;

import org.springframework.stereotype.Repository;

import com.jcg.examples.entity.Book;

public interface BookRepo extends CouchbaseRepository<Book, Long>

The repo simply extends from the interface and the implementation is provided by the Spring container at the run-time. We need to provide declare the proper Generics as per the Entity, which in our case is the . Spring Data Module provides us with a number of inbuilt method for manipulating the Data. We need not write the queries for basic data manipulation and reading.

For executing custom written queries, the developer can create his own method by specifying the Query using the annotation.

Lastly, and most important is the XML based configuration to initiate the Couchbase datasource so that we can communicate and execute our queries against the Server.


<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:beans=""


    <couchbase:clusterInfo  login="Administrator" password="Administrator"/>
	<beans:bean id="couchbaseEnv" class="" factory-method="create"/>
	<beans:bean id="myCustomTranslationService" class=""/>
	<couchbase:repositories base-package="com.jcg.examples.repo" />
	 <couchbase:template translation-service-ref="myCustomTranslationService"/>


    <couchbase:bucket bucketName="JavaCodeGeeks" bucketPassword="password.1"/>

That is all from the setup point of view. Now that all is set, let’s run the application and test out the code! Here’s ApplicationTest class that loads the XML file to instantiate the Spring Container and execute a few queries.

package com.jcg.examples;


import com.jcg.examples.entity.Book;
import com.jcg.examples.repo.BookRepo;

public class ApplicationTest

		public static void main(String[] args)
				ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new ClassPathResource("spring-couchbase-integration.xml").getPath());
				BookRepo bookRepo = context.getBean(BookRepo.class);
				Book book = new Book();
				book.setName("Kane And Abel by XYZ");;
				Book anotherBook = new Book();
				anotherBook.setName("The Prodigal Daughter");;
				Book retreivedBook = bookRepo.findOne(123l);


We have created two Book objects and persisted them as documents in the Database. Then we will try to retrieve them by their Id and delete one of them by passing their ID.

Here’s the sample output of the program:

INFO: Connected to Node
Feb 28, 2016 12:41:27 AM com.couchbase.client.core.config.DefaultConfigurationProvider$8 call
INFO: Opened bucket JavaCodeGeeks
Book [bookId=123, name=Kane And Abel by XYZ, isbnNumber=2133443554]

In the UI console, the user can see the save Documents under the Data-Buckets>Documents Tab. Here’s what it looks like :

Fig 2 : CouchBase Save Example
Fig 2 : CouchBase Save Example

And the detailed Document View:

Fig 3 : Saved Document details
Fig 3 : Saved Document details

4. Download the Source Code

Here we demonstrated how to configure and manage a Couchbase Database Server using Spring Data.

You can download the source code of this example here:

Chandan Singh

Chandan holds a degree in Computer Engineering and is a passionate software programmer. He has good experience in Java/J2EE Web-Application development for Banking and E-Commerce Domains.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
Manjesh Savita
Manjesh Savita
5 years ago

this example is not working

Back to top button