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

Building a Kubernetes cluster on Raspberry Pi (with automation)

Some months ago, I was lucky enough to get a bunch of Raspberry Pi minicompute…

What is a Database Proxy?

A proxy is a server software, typically installed in a separate machine, that …

ChatGPT as a MariaDB database

ChatGPT is truly impressive. You can instruct it to do all sorts of things whe…

Recent Comments

Contact Me