Searching...
Saturday, 9 November 2013

Spring Data MongoDB Simple Update Example

08:43

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

In this example we will see how to save and update Customer bean using SpringData and Mongodb.

Prerequisites: Mongodb should up and running

1. Create a Maven Project

Create a Maven project and add below dependencies

<repositories>
		<repository>
			<id>spring-milestone</id>
			<name>Spring Maven MILESTONE Repository</name>
			<url>http://maven.springframework.org/milestone</url>
		</repository>
	</repositories>
	<dependencies>
		
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>3.0.5.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>3.0.5.RELEASE</version>
		</dependency>
		<!-- mongodb java driver -->
		<dependency>
			<groupId>org.mongodb</groupId>
			<artifactId>mongo-java-driver</artifactId>
			<version>2.5.2</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-mongodb</artifactId>
			<version>1.0.0.M2</version>
		</dependency>
		<dependency>
			<groupId>cglib</groupId>
			<artifactId>cglib</artifactId>
			<version>2.2</version>
		</dependency>
	</dependencies>

2. Create a POJO (Customer.java)

package com.pretech;
public class Customer {
	public String name;
	public String address;
	public Customer(String name, String address) {
		super();
		this.name = name;
		this.address = address;
	}
	public Customer() {
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	@Override
	public String toString() {
		return "Customer [name=" + name + ", address=" + address + "]";
	}
}

3. Spring configuration (mongo-context.xml)


Place this file in to src/main/resources folder

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mongo="http://www.springframework.org/schema/data/mongo"
       xsi:schemaLocation="http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-3.0.xsd
          http://www.springframework.org/schema/data/mongo
          http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
          http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
       <!-- Default bean name is 'mongo' -->
       <mongo:mongo host="localhost" port="27017" />
       <bean id="customermongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate">
              <constructor-arg ref="mongo" />
              <constructor-arg name="databaseName" value="customerdb" />
              <constructor-arg name="defaultCollectionName" value="customerCollection" />
       </bean>
       <!-- To translate any MongoExceptions thrown in @Repository annotated classes -->
       <context:annotation-config />
</beans>

4. Create a Main class to save Customer details

package com.pretech;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.data.document.mongodb.MongoOperations;
import org.springframework.data.document.mongodb.query.Criteria;
import org.springframework.data.document.mongodb.query.Query;
import org.springframework.data.document.mongodb.query.Update;
public class SpringDataUpdateExample {
	public static void main(String[] args) {
		ApplicationContext ctx = new GenericXmlApplicationContext(
				"mongo-context.xml");
		MongoOperations mongoOperation = (MongoOperations) ctx
				.getBean("customermongoTemplate");
		Customer customer = new Customer("Vinod", "Bangalore");
		mongoOperation.save("customerDetails", customer);
		Customer savedCustomer = mongoOperation.findOne("customerDetails",
				new Query(Criteria.where("name").is("Vinod")), Customer.class);
		System.out.println("Customer details before update : " + savedCustomer);
		mongoOperation.updateFirst("customerDetails",
				new Query(Criteria.where("name").is("Vinod")),
				Update.update("address", "New Bangalore"));
		Customer updatedCustomer = mongoOperation.findOne("customerDetails",
				new Query(Criteria.where("name").is("Vinod")), Customer.class);
		System.out
				.println("Customer details after update : " + updatedCustomer);
	}
}

Output



Customer details before update : Customer [name=Vinod, address=New Software engineer]
Customer details after update : Customer [name=Vinod, address=New Bangalore]


0 comments: