Skip to main content

CRUD in MongoDB & Spring Data

Now, since we have already been introduced to mongo and its server is setup. We shall now move on to performing basic CRUD operations.
Lets take a use case example of ‘Company’ database that stores employee information. 

We wish to store employee name, email address, age and multiple addresses. Traditionally in RDBMS we would create an Employee table and Address Table having foreign key reference to ‘employee id ‘




Incase of NoSQL, we will be creating Employee documnet which will have store employee information like name, email, age and an array of employes address. Following is a snippet of the schema defined
{
"name" : "",
"email" : "",
"age" : “”,
"address" : [ ... ]
}
Thus to begin with, we will first define the entities. We have employee as an aggregate root entity that stores list of address having 1-M relatioship. Address Entity is represend as @Embeddable as it is embaded in another aggregate root entity. Employee is represented as @Document as the object to presist. The Employees id property is annotated with @Id so that JPA will recognize it as the object’s ID. The id property is also annotated with @GeneratedValue to indicate that the ID should be generated automatically.

Employee.java

Address.java

We have to specify the server host & port in the configuration file here localhost : 27017. Mongo Template offers convenience to create, update, delete and query for MongoDB documents and provides a mapping between your domain objects and MongoDB documents.Here, we will be creating & mapping employee bean which is our domain object.

ApplicationContext.xml

Now creating repository interface with CRUD methods.
 


EmployeeRepository.java
EmployeeRepositoryImpl.java





EmployeeMain.java

We shall also define a web service that would return the list of all employees and also an employee by id


EmployeeWS.java

EmployeeWSImpl.java

You can also find the complete source code at  https://github.com/meta-magic/MongoDB.git

Output
Displaying all Employees [
******************************************************************
     Employee Information :=1, name:=aaa, age:=10, email:= a@y.com
      Employee Address
     Address :=home addr, pin:=22235  
     Address :=ofc addr, pin:=21345    ,
******************************************************************
     Employee Information :=2, name:=bbb, age:=3, email:= b@y.com
      Employee Address
     Address :=home addr 2, pin:=22235  
     Address :=ofc addr 2, pin:=21345    ]
Find Employee with EMPID:1
******************************************************************
     Employee Information :=1, name:=aaa, age:=10, email:= a@y.com
      Employee Address
     Address :=home addr, pin:=22235  
     Address :=ofc addr, pin:=21345  
Displaying all Employees after update EMPID:1[
******************************************************************
     Employee Information :=1, name:=abc, age:=10, email:= a@y.com
      Employee Address
     Address :=home addr, pin:=22235  
     Address :=ofc addr, pin:=21345    ,
******************************************************************
     Employee Information :=2, name:=bbb, age:=3, email:= b@y.com
      Employee Address
     Address :=home addr 2, pin:=22235  
     Address :=ofc addr 2, pin:=21345    ]
Displaying all Employees after deleting EMPID:2 [
******************************************************************
     Employee Information :=1, name:=zzz, age:=10, email:= a@y.com
      Employee Address
     Address :=home addr, pin:=22235  
     Address :=ofc addr, pin:=21345    ]

Comments

Popular posts from this blog

Apache Kafka, A distributed Streaming Platform

This blog will guide 

1 - How to install Zookeeper on Linux/Mac.

2 - How to install Apache Kafka.

3 - Commands to produce and consume message on Kafka.



Installation of ZooKeeper Download latest zoopkeeper from http://zookeeper.apache.org/releases.html.Create opt/sotware folder in user directory, It can be any directory. I have extracted zookeeper  in /Users/ketangote/opt/zookeeper.cd /Users/ketangote/opt/zookeeper/confcp zoo_sample.cfg zoo.cfgOpen zoo.cfg using nano or vi and update data folder location:   dataDir=/Users/ketangote/opt/zookeeper/dataStart zookeeper commands: sh zkServer.sh start



Once zookeeper is started, use below command for verification.           telnet localhost 2181



Stop ZoopKeeper server : shZKServer.sh stop

Installation Of KAFKA
Download latest Kafka https://www.apache.org/dyn/closer.cgi?path=/kafka/0.10.1.0/kafka_2.11-0.10.1.0.tgzExtract folder in /opt folder.It can be any directory.Edit config/server.properties as below listeners=PLAINTEXT://:9099 broker.id=1Start ser…

Eureka-Server with spring cloud netflix

In this write-up we will focus on Service Registry – Eureka ServerRest service (auth-service application, eureka client) which register itself to registry.Web application which consumes Rest service using service registry.Service discovery allows services to find and communicate with each other without hardcoding hostname and port. Eureka Server


In spring-boot application enable the Eureka-Server by adding @EnableEurekaServer annotation to spring boot application.



We have put all the configuration on GIT and this is accessed using config-server. To now more about centralized configuration (config-server) click here
eurekaserver.yml

Place below bootstrap.yml in application, it basically connects to config-server and gets it required configuration.

Start the spring-boot application and access eureka server using http://localhost:8760/ you will get below screen.


Right now there is no application which is registered to eureka-server. Once you start the application which is register to eureka-ser…

Redis Basic CRUD

We have seen how to setup on your linux machine here , now we will see how to perform basic CRUD operations using Spring Data & Redis server
We will be creating a simple application that would persist an employee information into redis database. You will be needing certain JARs like jedis.jar, spring-data-redis.jar etc details of which you can download and view at https://github.com/meta-magic/RedisCRUDexample.git 
First of all we will be creating the Employee entity, plz note that we are using the Serializable interface which automatically mapps the objects against the key.


Employee.java



importjava.io.Serializable;publicclassEmployeeimplementsSerializable{privatestaticfinallong serialVersionUID =-8243145429438016231L;