Skip to main content

Introduction to JPA and ORM

When writing backend systems in Java, attempting to query a standard SQL database like MySQL or PostgreSQL utilizing raw JDBC connections is highly tedious.

You must manually write hardcoded SQL string queries (e.g., SELECT * FROM users), extract the resulting database row manually, and write 20 lines of code specifically mapping those String and Integer raw columns straight into a structured Java Object. This presents a massive disconnect called the "Object-Relational Impedance Mismatch."

What is an ORM?

An Object-Relational Mapping (ORM) framework solves this mismatch by attempting to transparently map structural Java Classes directly onto physical SQL database Tables without writing raw SQL. Hibernate is the most widely adopted ORM framework in the Java ecosystem.

What is JPA?

Java Persistence API (JPA) is merely an official Oracle specification/interface document. It dictates exactly how an ORM should behave in the Java enterprise suite, but it contains zero actual implementation code itself. Hibernate is a concrete implementation of the JPA specification.

Spring Data JPA

Spring Data JPA is a massive module adding a final protective abstraction layer sitting identically on top of Hibernate.

Instead of writing complex Hibernate configuration code, Spring Data JPA allows developers to define a standard generic Java Interface extending JpaRepository. Spring Boot intercepts the interface at runtime, inspects the generic type, and dynamically heavily injects the actual exact C.R.U.D. logic implementations natively behind the scenes.

// Spring generates the exact implementation code during runtime!
public interface UserRepository extends JpaRepository<User, Long> {
// You now instantly have access to .save(), .findById(), and .delete()
}