Jakarta Persistence (formerly known as JPA - Java Persistence API) is a part of the Jakarta EE (formerly Java EE) platform that provides a standard for object-relational mapping and managing relational data in Java applications. It allows developers to work with relational databases using Java objects, eliminating the need for extensive boilerplate SQL code.

Configuration

Before learning JPA, we have to provide some little configuration to our application

Spring JPA

H2 Database

Base Code

As we know Spring JPA is Object-Relational Mapping (ORM) Framework to map database tables to Java objects and vice-versa.

Student Model

It’s time to revisit our Student Model class and convert it into DAO.

Previously we was utilizing a HashMap as a database table which does not solve the problem of duplicity. That is why now we will set id as a Primary Key

package com.example.StudentTrack.Model;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;

@Entity
@Table(name = "Students")
public class Student {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private String email;
    private String course;

    // Constructors (Same as Previous)
    // Getters and Setters (Same as Previous)
}

In Above Code we have used the following annotations:

Repository

package com.example.StudentTrack.Repository;

import org.springframework.data.jpa.repository.JpaRepository;

import com.example.StudentTrack.Model.Student;

public interface StudentRepo extends JpaRepository<Student,Long>{
    // Additional custom query methods can be defined here if needed
}

JpaRepository provides all the basic CRUD functionality by default so we do not need to add anything but if you want to have some custom query such as findByEmail, you can add it in this interface.