Preparing to Use JPA
JPA is a foundational database-access technology. This page explains JPA and prepares a project for using it.
What Is JPA?
JPA (Java Persistence API) is a framework for accessing relational databases. JDBC has long been the common database-access approach in Java SE.
More precisely, JPA provides data persistence: data remains after the program exits. With JPA, application code stores and retrieves Java objects without manually writing SQL for each operation or extracting and converting every field.
Internally, JPA accesses the database. From the application’s perspective, it stores and retrieves Java objects. ORM (Object-relational Mapping) maps Java objects to relational database records. JPA offers similar object-oriented persistence behavior.
JPA also provides JPQL, an SQL-like query language. A consistent query style works across databases, so changing databases usually requires configuration changes rather than rewriting application code.
Creating a JPA Project
Create a web application project with Apache Maven.
$ mvn archetype:generate -DarchetypeArtifactId=maven-archetype-webapp
Enter:
groupId: com.devkuma.jpa
artifactId: JavaApp
version: 1.0-SNAPSHOT
package: com.devkuma.jpa
Folder Structure
.
├── pom.xml
└── src
└── main
├── resources
└── webapp
├── WEB-INF
│ └── web.xml
└── index.jsp
The webapp folder contains publicly served web application files. The resources folder contains configuration files required by JPA.
Writing pom.xml
pom.xml defines Maven build settings and required libraries.
Web Application Libraries
Use Jetty as the servlet container.
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>${jetty.version}</version>
</dependency>
Web Application Plugin
Add the Jetty Maven plugin to run the web application.
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty.version}</version>
</plugin>
Complete pom.xml
The complete project uses Jetty, H2, Eclipse Persistence JPA, and the Jetty Maven plugin.
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>com.devkuma.jpa</groupId>
<artifactId>JavaApp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jetty.version>9.4.7.v20170914</jetty.version>
</properties>
<dependencies>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.196</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.jpa</artifactId>
<version>2.7.0</version>
</dependency>
</dependencies>
<build>
<finalName>JavaApp</finalName>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty.version}</version>
</plugin>
</plugins>
</build>
</project>