Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Working with ORM: JPA and Hibernate | Working with Databases
Spring Boot Backend
course content

Conteúdo do Curso

Spring Boot Backend

Spring Boot Backend

1. Backend Development Basics
2. Spring Boot Basics
3. RESTful API
4. Working with Databases
5. Testing Backend Applications

Working with ORM: JPA and Hibernate

In essence, ORM is a concept that enables a Java object to be represented as data in a database (and vice versa). It is implemented in the form of the Java Persistence API (JPA) specification.

JPA provides an abstraction over SQL, allowing developers to interact with data through an object-oriented model. This means developers don't need to write complex SQL queries and can instead work with objects and their properties, making the code more readable and easier to maintain.

JPA

Real-life example

Imagine that JPA is like a standard for electrical outlets in different countries. This standard defines how an outlet should look and what specifications it must follow so that devices can be plugged into it.

However, the standard itself does not manufacture the outlets. To actually use them, you need manufacturers who implement this standard. They create the actual outlets that comply with it.

Note

Hibernate, for instance, is an implementation of this standard provided by JPA.

Main Components of the JPA

JPA components are key elements that work together to provide functionality for interacting with a database.

We'll explore these components through real-life examples to help you better associate them for future use.

Entity

In JPA, an entity is a class in your code that represents a table in the database. For instance, the User class describes what user-related data we store.

Imagine it as a business card. The card holds a person's name, job title, and contact information. In JPA, an entity class describes all the important characteristics of an object, just like a business card describes a person.

EntityManager

In JPA, the EntityManager performs operations such as adding, updating, or deleting records, managing all these actions.

EntityManager is responsible for managing the lifecycle of entities (objects) and their interaction with the database.

Note

The EntityManager is an interface that provides basic methods for working with entities and managing the entity context.

Main methods

Here’s how to use EntityManager in Spring Boot

java

BookService

copy
1234567891011121314151617181920212223242526272829
@Service public class BookService { @PersistenceContext private EntityManager entityManager; @Transactional public void createBook(String title) { Book book = new Book(title); entityManager.persist(book); // Save the new entity } public Book findBook(Long id) { return entityManager.find(Book.class, id); // Retrieve the entity by ID } @Transactional public void updateBook(Long id, String newTitle) { Book book = entityManager.find(Book.class, id); book.setTitle(newTitle); entityManager.merge(book); // Update the entity } @Transactional public void deleteBook(Long id) { Book book = entityManager.find(Book.class, id); entityManager.remove(book); // Remove the entity } }

The @PersistenceContext annotation allows developers to access the persistence context and perform operations on entities without manually creating an EntityManager.

We have been mentioning the persistence context, so let's clarify what it is!

Persistence Context

Lifecycle of Entities

  • Transient: The entity has just been created but has not yet been saved to the database;
  • Managed: The entity has been saved to the database and is being managed by the EntityManager. All changes to it are automatically tracked;
  • Detached: The entity was previously managed, but the persistence context has been closed or the EntityManager has been cleared. Changes in this state are not tracked automatically;
  • Removed: The entity has been marked for deletion from the database.

When the EntityManager creates or finds an entity, it places that entity into the persistence context.

All changes made to the managed entity are automatically tracked, and upon the completion of the transaction, these changes are synchronized with the database.

Creating SQL Queries

Using the methods of the EntityManager, we can also create queries directly within a method, and our entity will be placed into the persistence context.

java

BookService

copy
12345678910111213
@Service public class BookService { @PersistenceContext private EntityManager entityManager; public List<Book> findBooksByTitle(String title) { String jpql = "SELECT b FROM Book b WHERE b.title = :title"; TypedQuery<Book> query = entityManager.createQuery(jpql, Book.class); query.setParameter("title", title); return query.getResultList(); } }

In the BookService class, the findBooksByTitle method constructs a JPQL query to retrieve a list of Book entities with a specific title. It creates a TypedQuery, sets the title parameter, and executes the query to return the matching books from the database.

Note

As you can see, we are using JPQL instead of simple SQL for queries. Let's explore this in more detail.

JPQL (Java Persistence Query Language)

In JPQL, you write queries to find or modify data in the database using an object-oriented style.

In JPQL, we’re not dealing directly with the table and column names. Instead, we reference the Category class and its title field, making the query easier to understand for Java developers. JPQL abstracts away the underlying database structure, allowing you to focus on the object-oriented model.

Note

In the following chapters, we'll explore how to easily build such queries in more detail.

Hibernate

For example, Hibernate can automatically generate and update database tables based on your entities (classes).

Hibernate incorporates caching mechanisms that allow frequently used data to be stored in memory, reducing the number of requests to the database and speeding up application performance.

Note

Thus, Hibernate implements JPA while providing additional tools to enhance performance, flexibility, and ease of working with the database.

The Difference Between JPA and Hibernate

Imagine that JPA is like an instruction manual or a blueprint for working with databases in Java. This manual outlines the functions and interfaces that should be present, but it does not perform any work on its own.

Hibernate, on the other hand, is like a construction company that uses this manual to create a fully functioning system. Hibernate implements everything described in the JPA manual and adds its own additional features to make working with the database even more convenient and efficient.

Note

In other words, JPA sets the general rules, while Hibernate applies those rules and enhances them, ensuring actual data management.

1. What is JPA?
2. Which interface in JPA is used for performing operations on entities?

What is JPA?

Selecione a resposta correta

Which interface in JPA is used for performing operations on entities?

Selecione a resposta correta

Tudo estava claro?

Seção 4. Capítulo 4
We're sorry to hear that something went wrong. What happened?
some-alt