Open In App
Related Articles

What is Spring Data JPA?

Improve Article
Save Article
Like Article

JPA is a Java specification(Jakarta Persistence API) and it manages relational data in Java applications. To access and persist data between Java object(Plain Old Java object)/ class and relational database, we can use JPA. Upon  Object-Relation Mapping (ORM), it follows the mechanisms. It has the runtime EntityManager API and it is responsible for processing queries and transactions on the Java objects against the database. The main highlight is it uses JPQL (Java Persistent Query Language) which is platform-independent. JPA mainly covers persistence in terms of 

  • The Java Persistence API
  • Object-Relational metadata
  • Moreover under the persistence package API is defined.
  • We cannot say that JPA is a framework, but It defines a concept and it can be implemented by any framework.

Advantages of Using JPA

  • No need to write DDL/DML queries, instead we can map by using XML/annotations.
  • JPQL is used and since it is platform-independent, we no need to depend on any native SQL table. Complex expressions and filtering expressions are all handled via JPQL only.
  • Entity can be partially stored in one database like MySQL and the rest can be in Graph database Management System.
  • Dynamic generation of queries is possible.
  • Integration with Spring framework is easier with a custom namespace.

Units comprised in JPA are available under javax persistence package:

Persistence It has static methods to obtain an EntityManagerFactory instance
EntityManagerFactory Factory class for EntityManager and responsible for managing multiple instances of EntityManager
EntityManager It is an interface that works for the Query instance
Entity They are persistent objects and stored as records in the database
Persistence Unit Set of all entity classes
EntityTransaction It has one-to-one relationship with EntityManager.
Query To get relation objects that meet the criteria.

Many have a confusion between JPA and HIbernate. Let us see few key differences



JPA : It is a Java specification for mapping relational data in Java application. It is not a framework
Hibernate is an ORM framework and in that way data persistence is possible.
In JPA, no implementation classes are provided.
In Hibernate, implementation classes are provided.
Main advantage is It uses JPQL (Java Persistence Query Language)  and it is platform-independent query language.
Here it is using HQL (Hibernate Query Language).
It is available under javax.persistence package. 
It is available under org.hibernate package.
In Hibernate, EclipseLink, etc. we can see its implementation.
Hibernate is the provider of JPA.
Persistence of data is handled by EntityManager. Persistence of data is handled by Session.

Let us see a sample application for a spring boot application with JPA.

Example Project

It is a maven-driven project

Project Structure:

Project Structure




<?xml version="1.0" encoding="UTF-8"?>
    <description>Demo project for Spring Boot</description>
      <!-- Spring Boot provides starter dependency spring-boot-starter-data-jpa 
            to connect Spring Boot application with relational database efficiently.
           This is the mandatory one -->
          <!-- In memory database called apache derby is used 
                It is based on Java,JDBC and SQL standards
                Spring boot can auto configure with derby-->
        <!-- For testcase execution in spring boot project -->
            <name>Spring Milestones</name>
            <name>Spring Snapshots</name>
            <name>Spring Milestones</name>
            <name>Spring Snapshots</name>

Let us see the key important files in the project. Starting with POJO class


import javax.persistence.Entity;
import javax.persistence.Id;
// It should be marked as Entity,
public class GeekUserRecord {
    // Required attributes are given here. This will
    // comprise as collection of columns of a table
    @Id // For Primary key
    private int id;
    private String name;
    private String email;
    private String gender;
    private int numberOfPosts;
    // Getter and setter methods
    public String getGender() { return gender; }
    public void setGender(String gender)
        this.gender = gender;
    public int getNumberOfPosts() { return numberOfPosts; }
    public void setNumberOfPosts(int numberOfPosts)
        this.numberOfPosts = numberOfPosts;
    // default constructor is mandatory to specify
    public GeekUserRecord() {}
    public int getId() { return id; }
    public void setId(int id) { = id; }
    public String getName() { return name; }
    public void setName(String name) { = name; }
    public String getEmail() { return email; }
    public void setEmail(String email)
    { = email;

Let us see the controller file now


import com.gfg.model.GeekUserRecord;
import com.gfg.service.GeekUserService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
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.RestController;
// RestController = combination of the @Controller and the
// @ResponseBody It provides the response in the form of JSON
// or XML
public class GeekUserController {
    @Autowired private GeekUserService userService;
    // To get all geekusers
    public List<GeekUserRecord> getAllUser()
        return userService.getAllGeekUsers();
    // To add a geekuser, this is needed
    @RequestMapping(value = "/add-geekuser",
                    method = RequestMethod.POST)
    public void
    addUser(@RequestBody GeekUserRecord userRecord)

Let us see the service file


import com.gfg.model.GeekUserRecord;
import com.gfg.repository.GeekUserRepository;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
public class GeekUserService {
    private GeekUserRepository geekUserRepository;
    // for getting all the geekusers, we need to write this
    // method and output is GeekUserRecord as a list,
    public List<GeekUserRecord> getAllGeekUsers()
        List<GeekUserRecord> geekUserRecords
            = new ArrayList<>();
        return geekUserRecords;
    // While adding a geekuser, we need to save that
    public void addGeekUser(GeekUserRecord userRecord)

We need to add a repository file and it should extend CrudRepository


import com.gfg.model.GeekUserRecord;
public interface GeekUserRepository
    extends CrudRepository<GeekUserRecord, String> {

Now, we need to execute this program and check the output. For that, we need to run the below file


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
public class SpringDataJPAExampleApplication {
    public static void main(String[] args) {, args);

Right-click on the file and run the file as a Java application, we can see the output in the console.



The server started at port 8080

Initially as there are no records, when we hit http://localhost:8080, we wont be seeing any data. Let’s add the data by adding via the Postman client. Postman client has to be installed for doing this operation. URL to add users: http://localhost:8080/add-geekuser (Remember that this URL matches the controller file requestmapping).



Now a user is added. Hence we can verify the same by using http://localhost:8080


If we check with the added user detail and display user detail, they are same. If we have added more than 1 record, those records also will be displayed. In the whole project, we have not seen anywhere about the SQL statement like Insert(for adding)/Select(for retrieving). We need to have a POJO class. We have to possess the code with the important annotations as mentioned in the code. Automatically JPA (Java Persistence API) will take care of it.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 06 Jul, 2022
Like Article
Save Article
Similar Reads
Complete Tutorials