![]() |
VOOZH | about |
JPA (Java Persistence API) is a Java specification that makes it easier to work with relational databases. It lets you map Java classes (entities) to database tables and manage data using simple APIs instead of writing complex SQL. In short, JPA acts as a bridge between Java objects and database records, simplifying database operations in Java applications.
To work effectively with JPA, itβs important to understand its core concepts:
An entity is a lightweight, persistent domain object that represents a table in a database. Each instance of an entity corresponds to a row in that table.
Importance: Entities are the core components in the JPA as they can define the structure of the data in a JPA application.
Example Entity Class:
Explanation:
The EntityManager is the core interface in JPA for interacting with the persistence context. It allows you to create, read, update and delete entities, as well as manage transactions.
Importance: It acts as the primary API for database interactions and ensures entities are persisted properly.
Example of Using EntityManager:
The persistence context is a set of entity instances in which each entity identity is unique. It manages the lifecycle of entities and tracks changes made to them.
Importance: The persistence context ensures that entities are synchronized with the database and manages entity states such as managed, detached and removed.
The Criteria API is the programmatically defined the type safe query API used to define the queries for the entities and their persistent state of the JPA application.
Importance: It provides the platform for the creating dynamic queries where the structure of the query is not known until the runtime, unlike the JPQL which is static.
Example Criteria Query:
JPQL is the query language defined as the part of the JPA specification for the making queries against the entities stored in the database of JPA application.
Importance: JPQL can be designed to abstract database tables as the Java classes in the queries. Making it database agnostic and more integrated with the object oriented approach of the Java.
Example:
A persistence unit is a configuration in JPA that defines a set of entity classes managed by the EntityManager in your application.
Example in persistence.xml
Releted Articles:Architecture of JPA