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

bookWorking with ORM: JPA and Hibernate

As you already know, Object-Relational Mapping (ORM) is a technology that enables developers to interact with a database at the object level rather than through SQL queries.

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

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.

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

Let’s recall what an entity is. 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.

Main methods

Here’s how to use EntityManager in Spring Boot

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.

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.

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.

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

What is JPA?

Selecione a resposta correta

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

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

Selecione a resposta correta

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

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