VOOZH about

URL: https://www.geeksforgeeks.org/java/jpa-introduction/

⇱ JPA - Introduction - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

JPA - Introduction

Last Updated : 23 Aug, 2025

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.

Key features (significance) of JPA in Java

πŸ‘ significance_of_jpa_in_java
  • Data Integrity: Ensures that data is accurate and consistent across the database.
  • Allows Direct Mapping: Maps Java classes and fields directly to database tables and columns.
  • Portable: Works across different databases without changing the code.
  • Improves Productivity: Reduces boilerplate code and simplifies database operations.
  • Easy to Implement: Provides a standard and simple API for CRUD operations and queries.

Core Concepts of JPA

To work effectively with JPA, it’s important to understand its core concepts:

  • Entity
  • EntityManager
  • Persistence Context
  • Criteria API
  • JPQL (Java Persistence Query Language)
  • Persistence Unit

1. Entity

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:

  1. @Entity: Marks the class as a JPA entity. JPA will manage this object for persistence.
  2. @Table(name = "employees"): Maps the entity to the employees table. Optional if table name is same as class name.
  3. @Id: Marks the primary key field.
  4. @GeneratedValue(strategy = GenerationType.IDENTITY): Database auto-generates the ID.
  5. @Column(name = "name"): Maps a field to a specific column in the database. Optional if field name matches column name.

2. EntityManager

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:

3. Persistence Context

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.

4. Criteria API

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:

5. JPQL (Java Persistence Query Language)

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:

6. Persistence Unit

A persistence unit is a configuration in JPA that defines a set of entity classes managed by the EntityManager in your application.

  • It tells JPA which entities should be tracked and persisted.
  • It is configured in the persistence.xml file,
  • which contains details like the database connection, entity classes and JPA provider.

Example in persistence.xml

Releted Articles:Architecture of JPA

Comment
Article Tags:
Article Tags: