Stage 9: JDBC, JPA and Hibernate, lesson 2 of 6

JPA vs Hibernate: what's the difference?

Intermediate3 min read@since 17Code runs on your Java 25
Explain it forThe essentials plus production detail and pitfalls.

ORM (object-relational mapping) maps Java objects to database tables, so you work with objects instead of hand-writing SQL for every operation.

  • JPA, now called Jakarta Persistence, is a specification: annotations (@Entity, @Id) and interfaces (EntityManager). The package moved from javax.persistence to jakarta.persistence in version 3.0.
  • Hibernate is the most popular implementation of JPA (EclipseLink is another). It adds extras such as @BatchSize, @Formula, filters and its own Session API.
  • Spring Data JPA sits on top and generates repositories, so you often write no data-access code at all.

The stack: your code, then Spring Data JPA, then the JPA API, then Hibernate, then JDBC, then the database.

Example

Java
// Plain JPA: works with any provider
@PersistenceContext
EntityManager em;

@Transactional
public Long enroll(String email, Long courseId) {
    Course course = em.find(Course.class, courseId);
    Student s = new Student(email);
    s.enroll(course);
    em.persist(s);                       // INSERT happens at flush or commit
    return s.getId();
}

List<Course> popular = em.createQuery(
        "select c from Course c where c.rating >= :r order by c.rating desc", Course.class)
    .setParameter("r", 4.5)
    .setMaxResults(10)
    .getResultList();

Common mistake

Copying javax.persistence imports from old tutorials into a Spring Boot 3 or 4 project. Boot 3+ uses jakarta.persistence, so those imports won't compile.

Under the hood

Versions worth knowing: JPA 2.0 (2009) added the Criteria API; 2.1 added entity graphs and @Convert; 2.2 added java.time and stream results; Jakarta Persistence 3.0 renamed the packages; 3.2 (2024) brought record embeddables and JPQL set operations. Hibernate 6 rewrote the query engine (SQM); Hibernate 7 implements Jakarta Persistence 3.2 and is Apache-licensed. Spring Boot 3 and later require the jakarta namespace. See the Spring and JPA timeline on the Java versions page.

Check yourself

Which one is a specification rather than a library you run?

How this connects

Part of Job-ready backend developer.

Was this lesson helpful?

Finished reading? Mark it complete to track your progress.