Working with ORM: JPA and Hibernate
Deslize para mostrar o menu
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
JPA (Java Persistence API) is a Java EE specification for working with databases via ORM. It provides a standardized interface for interacting with data but does not implement the functionality itself.
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.
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
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.
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
Persistence Context
The persistence context is the environment in which the EntityManager manages the state of entity objects. It allows for the interaction with objects as if they were regular Java objects, abstracting the complexities of database interactions.
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
EntityManagerhas 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.
SELECT c from Category c WHERE c.title = 'query'
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.
PIn the following chapters, we'll explore how to easily build such queries in more detail.
Hibernate
Hibernate is a popular implementation of JPA that offers extended capabilities compared to the JPA specification itself. Hibernate is used to work with relational databases, automating many aspects of data management.
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?
Obrigado pelo seu feedback!
Pergunte à IA
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo