JQGrid to display Apache Solr Data

Hi all, from past couple of weeks i was doing a POC on Solr in my project and there came a scenario where i need to pull the data from Solr server and display on JQGrid component. In this post let me show you how to do this task. The project structure looks as below.

jqgrid_project

let’s start with our index.html file as below.

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Solr Data Drid</title>
<link href="css/jquery-ui-custom.css" media="screen" type="text/css" rel="stylesheet">
<link href="css/ui.jqgrid.css" media="screen" type="text/css" rel="stylesheet">
<script type="text/javascript" src="js/jquery-1.9.0.min.js"></script>
<script type="text/javascript" src="js/grid.locale-en.js"></script>
<script type="text/javascript" src="js/jquery.jqGrid.min.js"></script>
<script type="text/javascript" src="custom_js/app.js"></script>
</head>
<body>
<table id="list2"></table>
<div id="pager2"></div>
</body>
</html>

Now comes the time for coding the app.js file where the actual logic reside. follow the comments carefully and the script is easy to understand.

/**
 * @author Pavan Kumar Mantha
 */

$(document)
		.ready(
				function() {
					var dataTable = $("#list2");
					var entityArray = [];

					dataTable.jqGrid({
						datatype : "local",
						colNames : [ 'id', 'lastName', 'firstName',
								'titleOfCourtesy' ],
						colModel : [ {
							name : 'id',
							index : 'id',
							width : 55
						}, {
							name : 'lastName',
							index : 'lastName',
							width : 90
						}, {
							name : 'firstName',
							index : 'firstName',
							width : 100
						}, {
							name : 'titleOfCourtesy',
							index : 'titleOfCourtesy',
							width : 80,
							align : "right"
						} ],
						rowNum : 10,
						rowList : [ 10, 20, 30 ],
						pager : '#pager2',
						sortname : 'id',
						viewrecords : true,
						sortorder : "desc",
						autowidth : true,
						caption : "Solr Data"
					});

					// handles the grid resize on window resize
					$(window).resize(
							function() {
								dataTable.jqGrid('setGridWidth', parseInt($(
										window).width()) - 20);
							});

					// Firing Ajax request to Solr server and getting data to
					// populate the jqgrid.
					// This ajax request is powered by cross domain
					$.ajaxSetup({
						crossDomain : true,
						scriptCharset : "utf-8",
						contentType : "jsonp; charset=utf-8"
					});

					var request = $
							.ajax({
								url : "http://localhost:8983/solr/collection1/select?q=*%3A*&wt=json&json.wrf=?&indent=true&rows=100",
								mthod : "GET",
								dataType : "jsonp"
							});

					request.done(function(data) {

						$.each(data.response.docs, function(i, doc) {
							var entityobject = {
								id : "",
								lastName : "",
								firstName : "",
								titleOfCourtesy : ""
							}

							entityobject.id = doc.id;
							entityobject.lastName = doc.lastName;
							entityobject.firstName = doc.firstName;
							entityobject.titleOfCourtesy = doc.titleOfCourtesy;

							entityArray.push(entityobject);
						});

						for (var i = 0; i <= entityArray.length; i++)
							jQuery("#list2").jqGrid('addRowData', i + 1,
									entityArray[i]);
					});

					request
							.fail(function(msg) {
								alert("Fetching of data from solr failed with error !");
							});
				});// end of (document).ready(function(){});


That is it, finally when we deploy this project to server and see the output it should look as below.
jqgrid_op

In my next post will be posting about the integration of SpringData and Apache Solr until then.
Happy coding jqgrid 🙂

Advertisements

Spring Data JPA to persist the domain objects

Hi all,These days i am taking little time to post new topics as i am doing little R&D in spring’s latest projects like ‘SpringBoot’ and ‘SpringData’. Now here is the post which will explain about ‘SpringData’. Please follow the code little carefully as i wrote block comments in order to better understand the logic. this project is designed under Maven,Spring4.1.1.RELEASE, Eclipse Luna and Tomcat 8.0.11.

Please find the below is the project structure, First let us start with required dependencies for the project as shown below.
project_structure
pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.spark.spring.data.rest</groupId>
	<artifactId>SpringDataRest</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>SpringDataRest</name>
	<description>SpringDataRest</description>
	
	<repositories>
		<repository>
			<id>repository.jboss.org-public</id>
			<name>JBoss repository</name>
			<url>https://repository.jboss.org/nexus/content/groups/public</url>
		</repository>
	</repositories>
	<properties>
		<hibernate.version>4.0.1.Final</hibernate.version>
		<mysql.connector.version>5.1.9</mysql.connector.version>
		<slf4j.version>1.6.1</slf4j.version>
		<spring.version>4.1.1.RELEASE</spring.version>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>
	<dependencies>
		<!-- Spring Framework -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<!-- Spring MVC -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>cglib</groupId>
			<artifactId>cglib</artifactId>
			<version>2.2.2</version>
		</dependency>
		<!-- Spring Data JPA -->
		<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-jpa</artifactId>
			<version>1.0.2.RELEASE</version>
		</dependency>
		<!-- Hibernate -->
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>${hibernate.version}</version>
		</dependency>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-entitymanager</artifactId>
			<version>${hibernate.version}</version>
		</dependency>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-validator</artifactId>
			<version>4.0.1.GA</version>
		</dependency>

		<!-- H2 Database -->
		<!-- <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> 
			<version>1.3.160</version> </dependency> -->
		<!-- MySQL JDBC connector -->
		<!-- If you want to use MySQL, uncomment this dependency declation. -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>${mysql.connector.version}</version>
		</dependency>
		<!-- PostgreSQL JDBC 4 -->
		<!-- If you don't want to use PostgreSQL, uncomment this dependency declaration. -->
		<!-- <dependency> <groupId>postgresql</groupId> <artifactId>postgresql</artifactId> 
			<version>9.0-801.jdbc4</version> </dependency> -->
		<!-- BoneCP -->
		<dependency>
			<groupId>com.jolbox</groupId>
			<artifactId>bonecp</artifactId>
			<version>0.7.1.RELEASE</version>
		</dependency>
		<!-- Servlet API 3.0 -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.0.1</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>
		<!-- Logging dependencies -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>${slf4j.version}</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>${slf4j.version}</version>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.16</version>
		</dependency>
		<!-- Testing Dependencies -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.9</version>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<finalName>SpringData</finalName>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.3.2</version>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.1.1</version>
				<configuration>
					<failOnMissingWebXml>false</failOnMissingWebXml>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

Let us carry on with all the configurations required by the project and then with the actual logic. below is the code for the ‘application.properties’ file and ‘applicationContext.xml’
application.properties this file is fully loaded with all properties which are required by major databases, just uncomment what ever you need and use it.

# The default database is H2 memory database but I have also
# added configuration needed to use either MySQL and PostgreSQL.
 
#Database Configuration

#db.driver=org.h2.Driver
db.driver=com.mysql.jdbc.Driver
#db.driver=org.postgresql.Driver
#db.driver=oralce.jdbc.driver.OracleDriver

#db.url=jdbc:h2:mem:datajpa
db.url=jdbc:mysql://localhost:3306/test
#db.url=jdbc:postgresql://localhost/datajpa
#db.url=jdbc:oracle:thin:@localhost:1521:xe

db.username=?
db.password=?
 
#Hibernate Configuration
#hibernate.dialect=org.hibernate.dialect.H2Dialect
hibernate.dialect=org.hibernate.dialect.MySQLDialect
#hibernate.dialect=org.hibernate.dialect.OracleDialect
#hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect

hibernate.format_sql=true
hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy
hibernate.show_sql=true
 
#MessageSource
message.source.basename=i18n/messages
message.source.use.code.as.default.message=true
 
#EntityManager
#Declares the base package of the entity classes
entitymanager.packages.to.scan=com.spark.spring.data.domain

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
	
	<!-- Configures the location of static resources such as css files. Requires 
		Spring Framework 3.0 or higher. -->
	<mvc:resources mapping="/static/**" location="/static/" />

	<!-- Ensures that dispatcher servlet can be mapped to '/' and static resources 
		are still served by the containers default servlet. Requires Spring Framework 
		3.0 or higher. -->
	<mvc:default-servlet-handler />

	<!-- Configures Spring Data JPA and sets the base package of repositories. -->
	<jpa:repositories base-package="com.spark.spring.data.repositories" />

</beans>

Now it is time for us to write Java Configurations for Spring to initialize various bean objects and to recognize various annotations in the project, also let us write a configuration for servlet initialization instead of traditional web.xml

ApplicationConfig.java : this class is responsible for initialising the DataSource,JpaTransactionManager and for setting Hibernate properties

/**
 * 
 */
package com.spark.spring.data.config;

import java.util.Properties;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.hibernate.ejb.HibernatePersistence;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

/**
 * An application context Java configuration class. The usage of Java configuration
 * requires Spring Framework 3.0 or higher with following exceptions:
 * <ul>
 *     <li>@EnableWebMvc annotation requires Spring Framework 3.1</li>
 * </ul>
 * @author Pavan Kumar Mantha
 */
@Configuration
@ComponentScan(basePackages = {"com.spark.spring.data"})
@EnableWebMvc
@ImportResource("classpath:applicationContext.xml")
@PropertySource("classpath:application.properties")
public class ApplicationConfig {

	private static final String VIEW_RESOLVER_PREFIX = "/WEB-INF/jsp/";
    private static final String VIEW_RESOLVER_SUFFIX = ".jsp";
 
    private static final String PROPERTY_NAME_DATABASE_DRIVER = "db.driver";
    private static final String PROPERTY_NAME_DATABASE_PASSWORD = "db.password";
    private static final String PROPERTY_NAME_DATABASE_URL = "db.url";
    private static final String PROPERTY_NAME_DATABASE_USERNAME = "db.username";
 
    private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "hibernate.dialect";
    private static final String PROPERTY_NAME_HIBERNATE_FORMAT_SQL = "hibernate.format_sql";
    private static final String PROPERTY_NAME_HIBERNATE_NAMING_STRATEGY = "hibernate.ejb.naming_strategy";
    private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = "hibernate.show_sql";
    private static final String PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN = "entitymanager.packages.to.scan";
 
    @Resource
    private Environment environment;
    
    @Bean
    public DataSource getDataSource(){
    	DriverManagerDataSource dataSource = new DriverManagerDataSource();
    	dataSource.setDriverClassName(environment.getRequiredProperty(PROPERTY_NAME_DATABASE_DRIVER));
        dataSource.setUrl(environment.getRequiredProperty(PROPERTY_NAME_DATABASE_URL));
        dataSource.setUsername(environment.getRequiredProperty(PROPERTY_NAME_DATABASE_USERNAME));
        dataSource.setPassword(environment.getRequiredProperty(PROPERTY_NAME_DATABASE_PASSWORD));
 
        return dataSource;
    }
    
    @Bean(name="transactionManager")
    public JpaTransactionManager getJpaTransactionManager() throws ClassNotFoundException{
    	JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
    	jpaTransactionManager.setEntityManagerFactory(getLocalEntityManagerFactoryBean().getObject());
    	return jpaTransactionManager;
    }
    
    @Bean
    public LocalContainerEntityManagerFactoryBean getLocalEntityManagerFactoryBean(){
    	LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
    	entityManagerFactoryBean.setDataSource(getDataSource());
    	entityManagerFactoryBean.setPackagesToScan(environment.getRequiredProperty(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN));
    	entityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistence.class);
    	
    	Properties properties = new Properties();
    	properties.put(PROPERTY_NAME_HIBERNATE_DIALECT, environment.getRequiredProperty(PROPERTY_NAME_HIBERNATE_DIALECT));
    	properties.put(PROPERTY_NAME_HIBERNATE_FORMAT_SQL, environment.getRequiredProperty(PROPERTY_NAME_HIBERNATE_FORMAT_SQL));
    	properties.put(PROPERTY_NAME_HIBERNATE_NAMING_STRATEGY, environment.getRequiredProperty(PROPERTY_NAME_HIBERNATE_NAMING_STRATEGY));
    	properties.put(PROPERTY_NAME_HIBERNATE_SHOW_SQL, environment.getRequiredProperty(PROPERTY_NAME_HIBERNATE_SHOW_SQL));
    	
    	entityManagerFactoryBean.setJpaProperties(properties);
    	return entityManagerFactoryBean;
    }
    
    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
 
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix(VIEW_RESOLVER_PREFIX);
        viewResolver.setSuffix(VIEW_RESOLVER_SUFFIX);
 
        return viewResolver;
    }
}

WebContextInit.java : This class is responsible for initializing the servlet(front controller) and remember this will only work with servlet 3 containers only.

/**
 * 
 */
package com.spark.spring.data.config;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

/**
 * Web application Java configuration class. The usage of web application
 * initializer requires Spring Framework 3.1 and Servlet 3.0.
 * @author Pavan kumar Mantha
 */
public class WebContextInit implements WebApplicationInitializer {

	private static final String DISPATCHER_SERVLET_NAME = "dispatcher";
	private static final String DISPATCHER_SERVLET_MAPPING = "/rest/*";

	@Override
	public void onStartup(ServletContext servletContext)
			throws ServletException {
		AnnotationConfigWebApplicationContext annotationConfigWebApplicationContext = new AnnotationConfigWebApplicationContext();
		annotationConfigWebApplicationContext.register(ApplicationConfig.class);

		ServletRegistration.Dynamic dynamic = servletContext.addServlet(
				DISPATCHER_SERVLET_NAME, new DispatcherServlet(
						annotationConfigWebApplicationContext));
		dynamic.setLoadOnStartup(1);
		dynamic.addMapping(DISPATCHER_SERVLET_MAPPING);
		
	}

}

Now let us design our domain objects, data transfer objects and User defined Exceptions. In this case our domain object is “Person.java”. in this class we have a static inner class which is called “Builder”, the responsibility of Builder class is to create Person objects and return them.
Person.java

/**
 * 
 */
package com.spark.spring.data.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Version;

/**
 * An entity class which contains the information of a single person.
 * 
 * @author Pavan kumar Mantha
 */
@Entity
@Table(name = "person")
public class Person {
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;

	@Column(name = "first_name", nullable = false)
	private String firstName;

	@Column(name = "last_name", nullable = false)
	private String lastName;

	@Version
	private long version = 0;

	/**
	 * @return the id
	 */
	public Long getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(Long id) {
		this.id = id;
	}

	/**
	 * @return the firstName
	 */
	public String getFirstName() {
		return firstName;
	}

	/**
	 * @param firstName
	 *            the firstName to set
	 */
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	/**
	 * @return the lastName
	 */
	public String getLastName() {
		return lastName;
	}

	/**
	 * @param lastName
	 *            the lastName to set
	 */
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	/**
	 * @return the version
	 */
	public long getVersion() {
		return version;
	}

	/**
	 * @param version
	 *            the version to set
	 */
	public void setVersion(long version) {
		this.version = version;
	}
	
	/**
	 * this method returns the builder object
	 */
	public static Builder getBuilder(String firstName,String lastName){
		return new Builder(firstName, lastName);
	}
	
	/**
	 * A Builder class that is used to create Person Object.
	 */
	public static class Builder{
		
		Person person;
		
		public Builder(String firstName, String lastName) {
			person = new Person();
			person.firstName = firstName;
			person.lastName = lastName;
		}
		
		public Person build(){
			return person;
		}
	}

}

PersonDTO.java : This class is responsible to inject the data from the front end and then push that to builder class and get the original person domain object

/**
 * 
 */
package com.spark.spring.data.domain;

import org.hibernate.validator.constraints.NotEmpty;

/**
 * A DTO object which is used as a form object
 * in create person and edit person forms.
 * @author Pavan Kumar Mantha
 */
public class PersonDTO {

	private Long id;

	@NotEmpty
	private String firstName;

	@NotEmpty
	private String lastName;

	public PersonDTO() {

	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

}

PersonNotFoundException.java

/**
 * 
 */
package com.spark.spring.data.exceptions;

/**
 * @author Pavan Kumar mantha
 *
 */
public class PersonNotFoundException extends Exception{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public PersonNotFoundException() {
		super();
	}
	
	@Override
	public synchronized Throwable getCause() {
		return super.getCause();
	}
	
	@Override
	public String getMessage() {
		return super.getMessage();
	}
}

Since all our Domain Objects,DTO objects and custom exception types are in place, its time for us to write our Repository Interface. Spring Data has given an out of the box functionality for CRUD operations called CRUDRepository and there are many other Repositories which will extend from this base interface and one of them is “JpaRepository”. It is prety straight forward to design a repository all you have to do is to create an interface which extends the JpaRepository interface. The JpaRepository interface is a JPA specific extension to the Repository interface and it gives you the access to following methods which are used to implement the CRUD application:

delete(T entity) which deletes the entity given as a parameter.
findAll() which returns a list of entities.
findOne(ID id) which returns the entity using the id given a parameter as a search criteria.
save(T entity) which saves the entity given as a parameter.

PersonRepository.java

/**
 * 
 */
package com.spark.spring.data.repositories;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.spark.spring.data.domain.Person;

/**
 * Specifies methods used to obtain and modify person related information
 * which is stored in the database.
 * @author Pavan Kumar Mantha
 */
@Transactional(value="transactionManager",propagation=Propagation.NESTED)
public interface PersonRepository extends JpaRepository<Person, Long>{

}

Once we have our PersonRepository interface ready to use, let us start with service layer to hadle the business logic.
PersonService.java : This interface is responsible for basic operations on the domain object in connection with the underlying database.

/**
 * 
 */
package com.spark.spring.data.service;

import java.util.List;

import com.spark.spring.data.domain.Person;
import com.spark.spring.data.domain.PersonDTO;
import com.spark.spring.data.exceptions.PersonNotFoundException;

/**
 * Declares methods used to obtain and modify person information.
 * @author Pavan Kumar Mantha
 */
public interface PersonService {

	/**
     * Creates a new person.
     * @param created   The information of the created person.
     * @return  The created person.
     */
    public Person create(PersonDTO personDTO);
 
    /**
     * Deletes a person.
     * @param personId  The id of the deleted person.
     * @return  The deleted person.
     * @throws PersonNotFoundException  if no person is found with the given id.
     */
    public Person delete(Long personId) throws PersonNotFoundException;
 
    /**
     * Finds all persons.
     * @return  A list of persons.
     */
    public List<Person> findAll();
 
    /**
     * Finds person by id.
     * @param id    The id of the wanted person.
     * @return  The found person. If no person is found, this method returns null.
     */
    public Person findById(Long id);
 
    /**
     * Updates the information of a person.
     * @param updated   The information of the updated person.
     * @return  The updated person.
     * @throws PersonNotFoundException  if no person is found with given id.
     */
    public Person update(PersonDTO personDTO) throws PersonNotFoundException;
}

The implmentation for the above service interface is as below.
Note : all the methods of interface are implemented with default definition and only create has the exact logic, the others are left for you people to implement.Also observer @Transactional annotation it is at the method level not at the class level.
Please observer that the PersonRepository class gets injected by the spring container and CRUD operation capability is provided by it.
PersonServiceImpl.java

/**
 * 
 */
package com.spark.spring.data.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.spark.spring.data.domain.Person;
import com.spark.spring.data.domain.PersonDTO;
import com.spark.spring.data.exceptions.PersonNotFoundException;
import com.spark.spring.data.repositories.PersonRepository;
import com.spark.spring.data.service.PersonService;

/**
 * This implementation of the PersonService interface communicates with
 * the database by using a Spring Data JPA repository.
 * @author Pavan Kumar Mantha
 */
@Service(value="personService")
public class PersonServiceImpl implements PersonService{

	@Resource
	private PersonRepository personRepository;
	
	@Override
	@Transactional(value="transactionManager")
	public Person create(PersonDTO personDTO) {

		Person person = Person.getBuilder(personDTO.getFirstName(), personDTO.getLastName()).build();
		return personRepository.save(person);
	}

	@Override
	public Person delete(Long personId) throws PersonNotFoundException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Person> findAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Person findById(Long id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Person update(PersonDTO personDTO) throws PersonNotFoundException {
		// TODO Auto-generated method stub
		return null;
	}

}

Now every thing is completed from logic point of view let us start with the controller class class to handle different request from client.
Please observe that i have hard coded the values from domain object here, in my next post i will be making this as dynamic, for time being this is for testing.

/**
 * 
 */
package com.spark.spring.data.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.spark.spring.data.domain.PersonDTO;
import com.spark.spring.data.service.PersonService;

/**
 * @author Pavan Kumar Mantha
 *
 */
@Controller
public class ApplicationController {

	@Autowired
	private PersonService personService;
	
	@RequestMapping(value="/create",method=RequestMethod.GET)
	public void create(){
		System.out.println("-------------------inside the method-----------------"+personService);
		PersonDTO personDTO = new PersonDTO();
		personDTO.setFirstName("Pavan Kumar");
		personDTO.setLastName("Mantha");
		personService.create(personDTO);
	}
}

Thats it guys we are done with coding part, now build the project get the war file and deploy it to server.If you see no errors in server console please issue below request from browser “http://localhost:8080/SpringData/rest/create&#8221;.

in server you should see as below.
server_console

and when you query database that should look as below.
database_op

In my next post i will be integrating this project with AngularJs as my front end with fully Ajax driven application.

Happy Coding, Happy Spring Data 🙂