Introduction to Spring Data JPA

Introduction

Welcome to the first lesson of the course, "Persisting Data with Spring Data JPA." Throughout this course, you'll learn how to leverage Spring Data JPA to connect your application to relational databases, create simple and complex relations, implement pagination and sorting, and more. In this introductory lesson, we’ll explore the low-level way of working with relational databases using JDBC and contrast it with the high-level, more efficient way using JPA. We'll also introduce Spring Data JPA, a Spring module that simplifies database operations by building on top of JPA. Let's get started!

Understanding JDBC

Java Database Connectivity (JDBC) is a Java Standard Edition (SE) API that allows Java applications to interact with relational databases. It involves writing explicit SQL queries, managing database connections, and handling result sets manually. Here is an example of how to retrieve data using JDBC:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class JdbcExample {
    public static void main(String[] args) {
        String url = "jdbc:mysql://localhost:3306/mydb";
        String username = "root";
        String password = "password";

        try (Connection conn = DriverManager.getConnection(url, username, password);
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery("SELECT * FROM my_table")) {

            while (rs.next()) {
                System.out.println(rs.getString("column1"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

As you can see, this approach is cumbersome with a lot of boilerplate code. This method has nothing to do with Spring Boot and reflects the complexities involved in manual database operations.

If you want to dive deeper into JDBC, refer to the official specification.

Simplifying Database Access with JPA

The Java Persistence API (JPA) is a Java Enterprise Edition (EE) specification for Object Relational Mapping (ORM), which simplifies database interactions by mapping Java objects to database tables. ORM allows you to manipulate database records through Java objects, making your code cleaner and more maintainable. Here’s an example of a JPA entity class:

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

@Entity
public class Person {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private int age;

    // Getters and Setters
}

In this example:

  • @Entity: Marks this class as a JPA entity. This annotation will map the class to a database table named person (default naming convention).
  • @Id: Specifies the primary key of the entity. This annotation maps the id field to the primary key column of the person table.
  • @GeneratedValue(strategy = GenerationType.IDENTITY): Defines how the primary key is generated. Using IDENTITY strategy, the id field will be auto-incremented by the database.

So, this class will be mapped to a table named person, which will have columns id, name, and age, with id being the primary key.

For more information on JPA, consult the official specification.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal