Lesson 1
Introduction to Persistence with JPA in Jakarta EE 9
This module introduces the Java Persistence API (JPA), the standard for managing persistence in Jakarta EE 9 applications. You will learn how to define entities, use the EntityManager
for database operations, and integrate persistence with stateless session beans or CDI for business logic.
What is JPA?
The Java Persistence API (JPA), part of the
jakarta.persistence.*
package, is the modern standard for object-relational mapping (ORM) in Jakarta EE. Unlike the deprecated Entity Beans (Bean-Managed Persistence [BMP] and Container-Managed Persistence [CMP]) from older EJB specifications, JPA simplifies persistence with annotations, reducing boilerplate code and improving flexibility.
JPA allows developers to:
- Define entities using
@Entity
and map them to database tables.
- Perform CRUD (Create, Read, Update, Delete) operations using the
EntityManager
API.
- Leverage annotations like
@Id
, @Table
, and @Column
for fine-grained control over database mappings.
Note: Entity Beans, including BMP and CMP, were removed in Jakarta EE 9. BMP required developers to write explicit JDBC code to manage persistence, which was error-prone and tightly coupled to database schemas. JPA replaces these with a more robust and portable approach.
Why Use JPA?
JPA offers several advantages over deprecated Entity Beans:
- Simplified Development: Annotations like
@Entity
and @Id
reduce the need for complex XML configurations or manual JDBC code.
- Portability: JPA entities are database-agnostic, making it easier to switch between databases without code changes.
- Integration with Jakarta EE: JPA works seamlessly with CDI (
@ApplicationScoped
) and stateless session beans (@Stateless
) for business logic, as well as Jakarta RESTful Web Services for API development.
- Flexibility: JPA supports complex mappings, including relationships (
@OneToMany
, @ManyToOne
) and custom queries via JPQL or native SQL.
Creating and Managing Entities with JPA
Below is an example of a modern JPA entity and its integration with a stateless session bean for persistence operations.
Example: Defining a JPA Entity
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
// Getters and Setters
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}
Example: Managing Entities with a Stateless Session Bean
import jakarta.ejb.Stateless;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
@Stateless
public class UserService {
@PersistenceContext
private EntityManager entityManager;
public void createUser(User user) {
entityManager.persist(user);
}
public User findUser(Long id) {
return entityManager.find(User.class, id);
}
public void updateUser(User user) {
entityManager.merge(user);
}
public void deleteUser(Long id) {
User user = entityManager.find(User.class, id);
if (user != null) {
entityManager.remove(user);
}
}
}
Key JPA Operations
- Create: Use
EntityManager.persist()
to save a new entity to the database.
- Find: Use
EntityManager.find(Class<T>, Object primaryKey)
to retrieve an entity by its primary key.
- Update: Use
EntityManager.merge()
to update an existing entity.
- Delete: Use
EntityManager.remove()
to delete an entity.
- Querying: Use JPQL (Java Persistence Query Language) or native SQL for complex queries, e.g.,
entityManager.createQuery("SELECT u FROM User u WHERE u.name = :name", User.class)
.
Historical Context: Entity Beans vs. JPA
In older Java EE versions (EJB 2.x), Entity Beans were used for persistence. BMP required developers to write explicit JDBC code for database operations, while CMP relied on container-provided persistence logic. Both approaches were cumbersome:
- BMP tied
beans to specific database schemas, requiring code changes for schema updates.
- CMP relied on complex XML configurations and limited flexibility for non-relational data stores.
JPA, introduced in EJB 3.0, replaced Entity Beans and is now the standard for persistence in Jakarta EE. Entity Beans were deprecated in Jakarta EE 8 and removed in Jakarta EE 9, making JPA the recommended approach.
Next Steps
In the next lessons, you’ll learn:
- How to configure JPA in a Jakarta EE application using
persistence.xml
.
- How to use JPQL for advanced querying.
- How to integrate JPA with Jakarta RESTful Web Services for building APIs.
Conclusion
JPA is the cornerstone of persistence in Jakarta EE 9, offering a modern, annotation-driven approach to managing entities. By replacing deprecated Entity Beans, JPA simplifies development and enhances portability. The examples above demonstrate how to define and manage entities using JPA and stateless session beans, providing a foundation for building robust Jakarta EE applications.
