What is JPA?

JPA stands for Jakarta Persistence API (previously, Java Persistence API). It's an API specification for database connectivity in Java applications. Since JPA is only a specification, you need an implementation in order to use it. The most popular implementation is Hibernate ORM.

ORM stands for Object/Relational Mapping and is a technique to transform SQL tables to Java (or any other programming language) objects and vice versa. For example, maybe you have a programming_language table in the database and want to read rows from there and store them in Java objects of a class you created, for example, ProgrammingLanguage. What you have to do is use JPA annotations (or equivalent XML configurations, which, to be honest, it's been years since I saw a project using XML instead of JPA annotations) to tell the ORM framework how to put the data from the database in the Java fields and how to put data from the fields in the SQL table. Here's an example:

import jakarta.persistence.*;

@Table(name = "programming_language")
public class ProgrammingLanguage {
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "pl_id")
    private Integer id;
    @Column(name = "pl_name")
    private String name;
    @Column(name = "pl_rating")
    private Integer rating;
    ... equals, hashCode, setters, getters ...

This allows you to read and write data. For example:

EntityManagerFactory entityManagerFactory =
EntityManager entityManager = entityManagerFactory.createEntityManager();
EntityTransaction transaction = entityManager.getTransaction();
// write data:
entityManager.persist(new ProgrammingLanguage("Java", 10));
// read data:
List<ProgrammingLanguage> list = entityManager.createQuery(
        "select p from ProgrammingLanguage p where p.rating > 5",
transaction.commit(); // or transaction.rollback();

In order for this to work you also need to define a Persistence Unit. This is an XML file called persistence.xml that defines the name used to create the EntityManagerFactory in the previous snippet of code plus the database connection details:

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    <persistence-unit name="jpa-demo-local" transaction-type="RESOURCE_LOCAL">
            <property name="jakarta.persistence.jdbc.url" value="jdbc:mariadb://localhost:3306/jpa_demo"/>
            <property name="jakarta.persistence.jdbc.user" value="user"/>
            <property name="jakarta.persistence.jdbc.password" value="password"/>
            <property name="jakarta.persistence.schema-generation.database.action" value="drop-and-create"/>

This really was an extremely short introduction, but I recorded a more detailed yet still introductory video that demonstrates how to use JPA with Hibernate. Check it out:

If you liked this post, consider following me on Twitter and subscribing to Programming Brain on Youtube. Happy coding!

February 07, 2022


Popular Posts

How to call a Java method from a JavaScript function in the browser

In this video I demonstrate how to call a Java method that runs in the server f…

Testing MariaDB ColumnStore performance

MariaDB 's ColumnStore is an engine that stores data in a columnar fashion…

New YouTube channel on Programming (mostly Java)

Recently, I joined MariaDB Corporation as a Developer Advocate. My job is to h…

Recent Comments

Contact Me