Building Cloud Expertise with centron - Our Tutorials

Whether you are a beginner or an experienced professional, our practical tutorials provide you with the knowledge you need to make the most of our cloud services.

Spring Data JPA Overview

Spring Data JPA is part of Spring Data family. Spring Data makes it easier to create Spring driven applications that use new ways to access data, such as non-relational databases, map-reduction frameworks, cloud services, as well as well-advanced relational database support. This article will discuss about Spring Data JPA. We will also look into Spring Data JPA example application.


Some of the cool features provided by Spring Data JPA are:

  • Create and support repositories created with Spring and JPA
  • Support QueryDSL and JPA queries
  • Audit of domain classes
  • Support for batch loading, sorting, dynamical queries
  • Supports XML mapping for entities
  • Reduce code size for generic CRUD operations by using CrudRepository

When to Use it?

I would say that if you need to quickly create a JPA-based repository layer that is mainly for CRUD operations, and you do not want to create abstract DAO, implementing interfaces, Spring Data JPA is a good choice.


For our Spring Data JPA example, we will create a RESTful web service that will connect to Postgresql database. We will implement basic CRUD operations and work on a sample data we already have created.

Example Sample Data

Use below query to create table in Postgresql database and add some test data.

        create table people (
        id serial not null primary key,
        first_name varchar(20) not null,
        last_name varchar(20) not null,
        age integer not null

        insert into people (id, first_name, last_name, age) values
        (1, 'Vlad', 'Boyarskiy', 21),
        (2,'Oksi', ' Bahatskaya', 30),
        (3,'Vadim', ' Vadimich', 32);

Spring Data JPA Maven Project Structure

Below image shows the final Spring JPA project structure. We will look into each of the components in detail later on.


We need to add following dependencies for our Spring Data JPA example project.

  • postgresql: Postgresql java driver.
  • spring-core, spring-context: Spring Framework Core dependencies.
  • spring-webmvc, jackson-databind: For Spring REST application.
  • spring-data-jpa, hibernate-entitymanager: for Spring Data JPA and Hibernate support.

Below is content of final pom.xml build file.

        <project xmlns="" xmlns:xsi=""
            <name>Spring Data JPA Maven Webapp</name>

Spring Configuration Classes

package com.journaldev.spring.config;

import java.util.Properties;
import javax.sql.DataSource;
import org.hibernate.ejb.HibernatePersistence;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.transaction.annotation.EnableTransactionManagement;

public class DataConfig {
    private final String PROPERTY_DRIVER = "driver";
    private final String PROPERTY_URL = "url";
    private final String PROPERTY_USERNAME = "user";
    private final String PROPERTY_PASSWORD = "password";
    private final String PROPERTY_SHOW_SQL = "hibernate.show_sql";
    private final String PROPERTY_DIALECT = "hibernate.dialect";

    Environment environment;

    LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean lfb = new LocalContainerEntityManagerFactoryBean();
        return lfb;

    DataSource dataSource() {
        DriverManagerDataSource ds = new DriverManagerDataSource();
        return ds;

    Properties hibernateProps() {
        Properties properties = new Properties();
        properties.setProperty(PROPERTY_DIALECT, environment.getProperty(PROPERTY_DIALECT));
        properties.setProperty(PROPERTY_SHOW_SQL, environment.getProperty(PROPERTY_SHOW_SQL));
        return properties;

    JpaTransactionManager transactionManager() {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        return transactionManager;

@Configuration: this spring annotation says that it is configuration class.

@EnableTransactionManagement: this annotation allows users to use transaction management in application.

@EnableJpaRepositories(“com.journaldev.spring.repository”): indicates where the repositories classes are present.

@PropertySource(“”): says that we have property file in our classpath. The values from this file will be injected into environment variable. The contents of the file are shown below.



For using Spring Data, first of all we have to configure DataSource bean. Then we need to configure LocalContainerEntityManagerFactoryBean bean. We need this bean to control the entities. In this beans, you must specify the persistence provider i.e. HibernatePersistence in our case.

The next step is to configure bean for transaction management. In our example it’s JpaTransactionManager. Note that without configuring transaction manager we can’t use @Transactional annotation.

AppInitializer and WebConfig classes are to configure our application as web application without using web.xml file.

Model Class

package com.journaldev.spring.model;

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

@Table(name = "people")
public class Person {

    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "age")
    private Integer age;
    @Column(name = "first_name")
    private String firstName;
    @Column(name = "last_name")
    private String lastName;

    public Person() {

    public Long getId() {
        return id;

    public void setId(Long id) { = id;

    public Integer getAge() {
        return age;

    public void setAge(Integer age) {
        this.age = age;

    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;

    public String toString() {
        return "Person{" + "id=" + id + ", age=" + age + ", firstName='" + firstName + '\'' + ", lastName='" + lastName
                + '\'' + '}';

@Entity: This annotation allows entity manager to use this class and puts it in context.

@Table(name = “people”): associates a class with a table in the database.

@Id: says that this field is the primary key.

@GeneratedValue(strategy = GenerationType.IDENTITY): Defines the strategy for generating the primary key.

@Column(name = “age”): denotes a column in the database with which this field will be associated.

Spring Data JPA Repository

package com.journaldev.spring.repository;

import com.journaldev.spring.model.Person;
import java.util.List;

public interface PersonRepository

extends CrudRepository<Person, Long> { List findByFirstName(String firstName); }

By inheriting from CrudRepository, we can call many methods without the need to implement them ourself. Some of these methods are: save, findOne, exists, findAll, count, delete, deleteAll. We can also define our own methods. These method names should use special keywords such as “find”, “order” with the name of the variables. Spring Data JPA developers have tried to take into account the majority of possible options that you might need. In our example findByFirstName(String firstName) method returns all entries from table where field first_name equals to firstName. This is one of the most important feature of Spring Data JPA because it reduces a lot of boiler plate code. Also the chances of errors are less because these Spring methods are well tested by many projects already using them.

Spring Service Class


import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.journaldev.spring.model.Person;
import com.journaldev.spring.repository.PersonRepository;

public class PersonService {

    PersonRepository personRepository;

    public List getAllPersons() {
        return (List) personRepository.findAll();

    public List findByName(String name) {
        return personRepository.findByFirstName(name);

    public Person getById(Long id) {
        return personRepository.findOne(id);

    public void deletePerson(Long personId) {

    public boolean addPerson(Person person) {
        return != null;

    public boolean updatePerson(Person person) {
        return != null;

@Transactional annotation indicates that the method will be executed in the transaction. Spring will take care of transaction management.

Spring Controller Class

package com.journaldev.spring.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.journaldev.spring.model.Person;

public class PersonController {

    PersonService personService;

    @RequestMapping(value = "/person/{id}", method = RequestMethod.GET)
    public @ResponseBody Person getAllUsers(@PathVariable Long id) {
        return personService.getById(id);

    @RequestMapping(value = "/personByName/{name}", method = RequestMethod.GET)
    public List getPersoneByName(@PathVariable String name) {
        return personService.findByName(name);

    @RequestMapping(value = "/person", method = RequestMethod.GET)
    public List getAll() {
        return personService.getAllPersons();

    @RequestMapping(value = "/person/{id}", method = RequestMethod.DELETE)
    public HttpStatus deletePersnone(@PathVariable Long id) {
        return HttpStatus.NO_CONTENT;

    @RequestMapping(value = "/person", method = RequestMethod.POST)
    public HttpStatus insertPersone(@RequestBody Person person) {
        return personService.addPerson(person) ? HttpStatus.CREATED : HttpStatus.BAD_REQUEST;

    @RequestMapping(value = "/person", method = RequestMethod.PUT)
    public HttpStatus updatePerson(@RequestBody Person person) {
        return personService.updatePerson(person) ? HttpStatus.ACCEPTED : HttpStatus.BAD_REQUEST;

Final step is to create the controller class to expose our APIs to outer world.

Spring Data JPA Testing

Just build and deploy the project into your favorite servlet container such as Tomcat. Below images show the response for some of the API calls.

Spring Data JPA Read All

Spring Data JPA Get By Name

Spring Data JPA Create

Spring Data JPA Update

Spring Data JPA Delete

That’s all for Spring Data JPA Guide. Spring Data JPA Overview

Start Your Free Trial and Elevate Your Data Management with Our Cloud-Solutions

Ready to revolutionize your Java database interactions? Dive into the world of efficient data handling with our cloud-platform. Sign up now for a free trial and experience seamless integration, enhanced performance, and the ease of managing your data on the cloud. Don't miss the opportunity to transform your data management strategy - your journey towards streamlined database solutions begins here!

Try for free!