What is the Java Persistence API (JPA)

The Java Persistence API (JPA) is a specification for accessing, persisting, and managing data between Java objects and relational databases. It provides a standardized way to handle database interactions in Java applications and is a part of the Java EE (Enterprise Edition) platform. JPA simplifies data manipulation and retrieval while providing a framework for working with different types of databases.

JPA uses the concept of an entity, which represents a table in a database, and a persistence context, which is a set of entity instances that the EntityManager is managing. JPA also supports a query language (JPQL) that allows for powerful querying capabilities.

Here’s an example of how to use JPA with a simple entity:

@Entity public class Customer { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String name; private String email; // 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; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }

Java Persistence API JPA Entity Persistence Context JPQL Java EE