AI Hints
Home›Spring Boot Tutorial›Why a Database, The H2 Database and What JPA Is
Spring Boot · Lesson 48 of 120

What JPA Is

Objects instead of SQL.

⏱ 2 min read▶ Video at 1:04:26🎓 Free
Watch this lesson in the Spring Boot courseWatch on YouTube ↗Full Spring Boot course

Writing SQL for every save, and every search, gets tiring. And it's easy to make mistakes. JPA lets us work with Java objects instead, and writes the SQL for us.

  • SQL everywhere is tiring
  • Easy to make mistakes
  • Work with objects

Object relational mapping

A Java class is mapped to a table. Each field is mapped to a column. When we save an object, JPA writes an insert. When we load one, it writes a select, and builds the object. This is called object relational mapping, or ORM.

  1. class Book → table book
  2. field → column
  3. save → INSERT
  4. find → SELECT
  5. ORM

Three names

JPA is the standard, a set of rules and annotations, for mapping objects. Hibernate is the library that actually does the work. And Spring Data JPA sits on top, and writes even the repository code for us.

NameWhat it is
JPA (Jakarta Persistence)the standard annotations
Hibernatethe engine that runs it
Spring Data JPArepositories written for you

With and without JPA

Without JPA, we write SQL strings, and copy each column into an object by hand. With JPA, we annotate the class once. Then we just call save, and find. The SQL is generated for us.

JDBC / SQL

  • "INSERT INTO book ..."Copy columns by handLots of code

JPA

  • @Entity class Bookrepository.save(book)Almost no code

Summary

JPA maps classes to tables, and fields to columns. Hibernate runs it, and Spring Data JPA writes repositories for us. Next, entities.

Key takeaways
  • ORM
  • Hibernate
  • Spring Data JPA
Next up: Entities — classes that become tables