0
0
Spring Bootframework~5 mins

Why JPA matters for database access in Spring Boot

Choose your learning style9 modes available
Introduction

JPA helps your app talk to databases easily without writing lots of SQL. It makes saving and getting data simple and organized.

When you want to store user info like names and emails in a database.
When your app needs to update or delete records without complex SQL.
When you want to switch databases later without changing much code.
When you want to work with data as objects instead of tables and rows.
Syntax
Spring Boot
import jakarta.persistence.Entity;
import jakarta.persistence.Id;

@Entity
public class User {
    @Id
    private Long id;
    private String name;

    // getters and setters
}
Use @Entity to mark a class as a database table.
Use @Id to mark the primary key field.
Examples
This defines a Product table with productId as the key.
Spring Boot
@Entity
public class Product {
    @Id
    private Long productId;
    private String productName;
}
This interface lets you save, find, and delete User objects easily.
Spring Boot
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository<User, Long> {
    // Spring Data JPA creates methods automatically
}
Sample Program

This Spring Boot app saves a User to the database and then finds it by ID. It prints the user's name.

Spring Boot
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;

@SpringBootApplication
public class DemoApplication implements CommandLineRunner {

    @Autowired
    private UserRepository userRepository;

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        User user = new User(1L, "Alice");
        userRepository.save(user);
        User found = userRepository.findById(1L).orElse(null);
        System.out.println("Found user: " + (found != null ? found.getName() : "none"));
    }
}

@Entity
class User {
    @Id
    private Long id;
    private String name;

    public User() {}

    public User(Long id, String name) {
        this.id = id;
        this.name = name;
    }

    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
}

interface UserRepository extends JpaRepository<User, Long> {}
OutputSuccess
Important Notes

JPA hides complex SQL behind simple Java methods.

Spring Data JPA creates database methods automatically from interfaces.

Always mark your entity classes with @Entity and provide an @Id.

Summary

JPA makes database access easier by using Java objects.

It helps you avoid writing SQL directly.

Spring Data JPA adds even more convenience with ready-made methods.