9.1 Overview of ORM and JPA
In the Spring ecosystem, the most modern and mainstream technology for handling databases is JPA (Java Persistence API).
Paradigm Mismatch
For a long time, Java developers beautifully drafted business logic using Object-Oriented Programming (OOP) only to find that when saving data, they had to break it down to fit the 2-dimensional table structure of a Relational Database Management System (RDBMS) and manually write long SQL queries (INSERT INTO ...).
There's a significant structural difference (Paradigm Mismatch) between the Object Model and the Relational Database Model, such as inheritance, association relationships, and directionality. Developing primarily with SQL naturally shifts the developer's mindset toward table-centric rather than object-centric design.
The Advent of ORM (Object-Relational Mapping)
To resolve this issue, ORM technology emerged. ORM is an automated mapping tool linking an application's "Objects" with the database's "Relational Tables."
When an object is saved simply by invoking a save method—much like placing it into a Java collection (List, Map)—the ORM framework analyzes this, dynamically generates the appropriate SQL query, and shoots it off to the database.
JPA, Hibernate, and Spring Data JPA
It's necessary to clarify these three terms:
- JPA (Java Persistence API): A standard specification (a collection of interfaces) in the Java ecosystem for ORM technology.
- Hibernate: The most popular and powerful "implementation" of the JPA interface specification.
- Spring Data JPA: A "module" within Spring that wraps JPA/Hibernate to make it even easier to use. (The magical entity that generates queries just by defining a Repository interface.)
// With Spring Data JPA, queries auto-generate based purely on method names
List<User> findByAgeGreaterThan(int age);
In short, we use the Hibernate implementation, which conforms to the JPA specification, handled via the highly convenient Spring Data JPA tool. Through this, we break free from writing repetitive, simple CRUD SQL queries and focus entirely on core business logic.