🎓 A1Learner
🔍
0 of 50 lessons completed

What JPA Is

JPA is a Java standard for saving objects in a database. A database is a store that keeps data after the app stops.

The starter that adds JPA

This dependency adds JPA and Hibernate. Add it to pom.xml.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

H2 runs inside the app

These two lines point JPA at H2. The data lives in memory.

spring.datasource.url=jdbc:h2:mem:tasks
spring.jpa.hibernate.ddl-auto=create-drop

Example

public class JpaCheck {

    public static void main(String[] args) throws Exception {
        Class.forName("jakarta.persistence.Entity");
        System.out.println("JPA is on the classpath");
    }
}

Key points

  • JPA maps a Java class to a database table.
  • A table is a set of rows that hold data.
  • Each object becomes one row in the table.
  • Each field becomes one column in the row.
  • SQL is the language that databases speak.
  • JPA writes the SQL for saving and reading.
  • Hibernate is the JPA tool that Spring Boot uses.
  • H2 is a small database that lives inside the app.

In this example, the main method runs first. Class.forName looks for the JPA Entity type. The type is on the classpath when the starter is present. The print states the result.

Note

H2 keeps the data only while the app runs.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

What does JPA map a Java class to?