Thursday , August 24 2017
Home / Spring Boot / Spring Boot JPA Integration Example

Spring Boot JPA Integration Example

In this tutorials, we are going to show you how to integrate Spring Boot with data JPA as  Spring Boot JPA Example. This includes how to integrate the simple spring boot application with Spring Data JPA.

Spring Boot JPA Integration

Spring Boot JPA Integration :

Here we are going to use the below technologies:

  • Java 8
  • Spring Boot 1.5.1.RELEASE
  • Spring Data JPA 1.11.0.RELEASE
  • Mysql

Spring Boot JPA Project Structure :

A typical maven project directory structure,

Spring Boot JPA

Required Dependencies :

Here, the most important thing is to defining the dependencies for Spring Boot JPA related.

Unlike the spring mvc with hibernate integration, we don’t need here a separate dependencies for spring boot and spring data jpa.

Hence we can define a single spring artifactid as spring-boot-starter-data-jpa, it will grasp all the dependencies for Spring boot and data jpa.

Since we are working with Mysql data base, we should declare the dependency for mysql connector too.

Recommended : Spring Boot with JPA multiple Data Sources Example


<project xmlns="" xmlns:xsi=""
        <!-- Generic properties -->


            <name>Spring Releases</name>
            <name>JBoss Maven Release Repository</name>

            <name>Spring Releases</name>

This example is to connecting with Spring Boot JPA application with Mysql database, I am going to access the below person table.

Create person table :


Create a properties file with data base connection properties :


Here the actual magic will come in to picture! Spring boot provides a bunch of application level properties to manage our application configurations.

By using this properties, we can eliminate the boiler plated code for creating the database configurations like datasources and transactions. Spring boot will take the responsibility to create all these kinds of stuff for us. Hence there is no boiler plate data source configurations here.

Creating a repository interface.

package com.onlinetutorialspoint.repository;
import org.springframework.stereotype.Repository;

import com.onlinetutorialspoint.entity.Person;
public interface PersonRepository extends CrudRepository<Person, Long>{

Here @Repository is a spring stereo type annotation , used to tell the spring context, it is a persistent layered class.

CrudRepository is an interface given by the spring data, which gives us the basic crud operations on a repository for a specific type. Here in our example the specific type is Person class.

In any typical hibernate using applications like spring with hibernate, we need to write all the CRUD operations of our own. Where as in Spring data JPA, CrudRepository provides sophisticated  CRUD operations for entity class.

Creating Entity class :

It is a typical JPA entity class.

package com.onlinetutorialspoint.entity;

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 = "person")
public class Person {
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private String city;

    public Person() {
        // TODO Auto-generated constructor stub

    public Long getId() {
        return id;

    public void setId(Long id) { = id;

    public String getName() {
        return name;

    public void setName(String name) { = name;

    public String getCity() {
        return city;

    public void setCity(String city) { = city;

    public String toString() {
        return "Person [pid=" + id + ", pName=" + name + ", pCity=" + city
                + "]";


Creating a service class :

package com.onlinetutorialspoint.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.onlinetutorialspoint.dto.PersonDTO;
import com.onlinetutorialspoint.entity.Person;
import com.onlinetutorialspoint.repository.PersonRepository;

public class PersonService {
    PersonRepository personRepo;

    public void savePersonDetails(PersonDTO personDto) {
        try {
            Person person = new Person();
        } catch (Exception e) {

    public List<Person> getAllPersons() {
        return (List<Person>) personRepo.findAll();

    public Person getPerson(long id) {
        return personRepo.findOne(id);

    public Person savePerson(Person person) {

Here @Service is a spring stereo type annotation , used to tell the spring context, it is a service layered class.

Creating DTO class :

package com.onlinetutorialspoint.dto;

public class PersonDTO {
    private Long pid;
    private String pName;
    private String pCity;
    public Long getPid() {
        return pid;
    public void setPid(Long pid) { = pid;
    public String getpName() {
        return pName;
    public void setpName(String pName) {
        this.pName = pName;
    public String getpCity() {
        return pCity;
    public void setpCity(String pCity) {
        this.pCity = pCity;

Spring Boot Application class :

package com.onlinetutorialspoint;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import com.onlinetutorialspoint.entity.Person;
import com.onlinetutorialspoint.repository.PersonRepository;
import com.onlinetutorialspoint.service.PersonService;

public class Application {
    public static void main(String[] args) {;
    PersonService personService;
    public CommandLineRunner run(PersonRepository repository) {
        return (args) -> {
            Person person = new Person();
            person.setName("Chandra Shekhar Goka");
            Person p = savePersonDetails(person);
            System.out.println("Person Id : "+p.getId() +" Person Name : "+p.getName());
    public Person savePersonDetails(Person p){
        return personService.savePerson(p);
    public Person getPerson(Person person){
        return personService.getPerson(person.getId());

@SpringBootApplication annotation tells to Spring Boot, this is a starting point of our Spring Boot Application.

CommandLineRunner is an interface provided by the spring boot. It is used to indicate that a bean should run when it is contained within a spring application.

In run method I have created  a Person class and call a savePerson method on PersonService class in Java 8 lambda expression to save the person details.

Lets run the to save the person details. You can run this application as simple as Java standalone application by running the main class.

If every thing goes well you will see the output like this:

Spring Boot JPA Output

On the above output you can see the SQL command for insert person data.

Happy Learning:)

Download Example

About chandrashekhar

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,


How to change Spring Boot Tomcat Port Number

In this tutorials, I am going to show you how to change the Spring Boot …

Leave a Reply

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