What is JPA (Java Persistence API)?

JPA is a Java specification for object-relational mapping (ORM) in Java. JPA provide a way to map Java objects to database tables. This allows programmers to manipulate database information directly using Java objects instead of executing database SQL queries.

Developer can choose one of many available JPA specification implementation libraries such as Hibernate, Apache OpenJPA and EclipseLink. EclipseLink is the reference implementation of the JPA specification. In the examples that we are going to provide you in this website, Hibernate library will be used as the persistence provider.

In JPA we model our database tables into a Java objects. This Java objects also called as entity objects. The entity represent a table in database. A single row in a database table will be represented in an instance of the entity. This entity objects hold information about the mapping between objects and database tables. This information or metadata can be defined using an annotation or an XML mapping files.

Here is a simple example of entity object and its metadata information.

package org.kodejava.example.model;

import javax.persistence.*;
import java.io.Serializable;

@Table(name = "genres")
public class Genre implements Serializable {
    private static final long serialVersionUID = 6986841075572235403L;

    private Long id;
    private String name;

    public Genre() {

    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId() {
        return id;

    @Column(nullable = false, length = 50)
    public String getName() {
        return name;

    public void setId(Long id) {
        this.id = id;

    public void setName(String name) {
        this.name = name;

In the sample entity above we use annotation such as @Entity, @Table, @Id, @GeneratedValue and @Column. These are some annotations that you can use for object mapping.

Besides manipulating database tables using objects JPA also provide a SQL-like queries that can be use to create a static or dynamic query statement.

Wayan Saryada

Wayan Saryada

A programmer, runner, recreational diver, currently living in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, Hibernate / JPA.
Wayan Saryada

Leave a Reply