Couchbase – How to create Spring JPA Couchbase application with SpringBoot

Couchbase server is an open source, distributed multi-model NoSQL document-oriented database. So in the tutorial, JavaSampleApproach will show you how to create a Spring JPA Couchbase with SpringBoot.

Related posts:
Couchbase – How to create Spring Cache Couchbase application with SpringBoot
How to use SpringData MongoRepository to interact with MongoDB
How to start Spring Data Cassandra with SpringBoot

I. Technologies

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.5.6RELEASE
– Couchbase 4.6.2

II. SpringBoot & Couchbase

Spring Boot provides auto-configuration for Couchbase and abstractions on top of it by Spring Data Couchbase. We can use it easily via spring-boot-starter-data-couchbase ‘Starter’ which had collected the needed dependencies.

Spring JPA Couchbase - SpringBoot - Architecture

For connecting with Couchbase Bucket and Cluster, we can use spring.couchbase.* properties in application.properties file:

For working with Couchbase repository, we use CouchbaseRepository interface:

***Note*** We can meet some problems when working with Couchbase database:

1. No view configuration => Exception:

2. No index => Exception:

Solution:
– Manually setup Couchbase view and create index
or
– Using SpringBoot support: @N1qlPrimaryIndexed annotation & configure spring.data.couchbase.auto-index=true to create views and indexs.
spring.data.couchbase.auto-index is used to automatically create views and indexes. Use the meta-data provided by @ViewIndexed, @N1qlPrimaryIndexed and @N1qlSecondaryIndexed.

III. Practice

In the tutorial, we setup Couchbase 4.6.2 server and use SpringToolSuite to create SpringBoot project:

spring jpa couchbase - project structure

Step to do:
– Setup Couchbase 4.6.2 server
– Create SpringBoot project
– Create Couchbase model
– Create Couchbase repository
– Implement client
– Run & check results

1. Setup Couchbase 4.6.2 server

Download Couchbase at https://www.couchbase.com/downloads

couchbase download

Press Download -> Fill Info -> Try it now:

couchbase download - fill info

-> We got couchbase-server-enterprise_4.6.2-windows_amd64.exe file.

couchbase installer

Note: turn-off your virus protection programs before setup.

Right click on the installer for installing. Follow the installer prompts,

couchbase install

couchbase install-required

-> We got an Web-UI setup: at http://localhost:8091.

install couchebase server - web setup

Press Setup button.

couchbase server setup - web ui - 1
couchbase server setup - web ui - 2

Click Next to accept the default values for new cluster.

couchbase server setup - web ui - sample buckets - sample bucket

On Sample Buckets screen, click choose beer-sample and travel-sample and click Next, we got:

couchbase server setup - web ui - step 3-5

On above Create Default Bucket, under Memory Size set the Per Node RAM Quota to 100 MB and click Next, we got:

couchbase server setup - web ui - step 4-5

On above Notifications screen, enter your registration information then click Next, we got:

couchbase server setup - web ui - step 5-5

On the Configure Server screen, enter and verify a password for the administrator account, and click Next.
The Couchbase Web Console opens and displays the Cluster Overview:

couchbase server setup - web ui - final

-> Now Couchbase server is ready for use.

– Create jsabucket:

create jsa bucket

create jsa bucket - result

2. Create SpringBoot project

Using SpringToolSuite, create a SpringBoot project, then add needed dependency spring-data-couchbase:

3. Create Couchbase model

Create a Couchbase model Customer:

4. Create Couchbase repository

– Create CustomerRepository repository:

– Open application.properties, add connection config:

5. Implement client

Using above CustomerRepository to implement client:

6. Run & check results

Build and Run the SpringBoot project with commandlines {mvn clean install, mvn spring-boot:run}.

– Results:

-> Logs:

-> all view is auto-created by the SpringBoot application.

couchbase productview - all

-> Query with Couchbase:

couchbase select all

IV. Sourcecode

SpringBootCouchbase

By JavaSampleApproach | August 22, 2017.


Related Posts


1 thought on “Couchbase – How to create Spring JPA Couchbase application with SpringBoot”

Got Something To Say:

Your email address will not be published. Required fields are marked *

*