Getting Started with Maven | Using Databases | Creating an H2 + JPA Project

During development, Maven automatically downloads the required libraries from the central repository and builds the project. Therefore, even when using a library in development, if you are using Maven, you do not need to visit the library’s site, download it, and add it to the project. If you write the library in pom.xml and build, all libraries are automatically referenced and used.

Now let’s try development using an actual library. A common kind of library used in Java applications is related to “databases.”

Here, we will use the “H2” database engine. H2 is a pure Java SQL database library. Because it is pure Java, it can be used directly inside Java programs. H2 can also store data in files or in memory. During development, you can store data in memory, and for release, you can store it in files.

Searching the Central Repository

To use H2, let’s check how to write the <dependency> tag. First, access the site for searching the central repository.

https://search.maven.org/

Access it, enter “h2” in the input field, and search. A list of search results appears. Find the item with group ID “org.h2database” and artifact ID “h2”, then click the version number link for “Latest Version.” The version information is displayed.

There, code for adding the library with major build tools is organized by tool. Under “Dependency Information,” the “Apache Maven” section lists Maven’s <dependency> tag. Copy it and paste it inside <dependencies> in pom.xml to add the H2 library to the project.

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>1.4.196</version>
</dependency>

To use a library with Maven, search for the library on Maven’s search site, copy its Dependency Information, and paste it into pom.xml. Once you get used to it, it is simple.

Completing the Project and pom.xml

Now create the project. This time, you can reuse the SampleMavenApp project used earlier. If you want to create a new project, create one with the mvn archetype:generate command. In that case, specify -DarchetypeArtifactId=maven-archetype-quickstart when running it.

Once the project is ready, complete pom.xml. The <dependency> tag for the H2 library was easy to prepare, but other libraries are also required. This time, we will access the database using JPA. For that, javax.persistence and Persistence JPA must be prepared.

The completed pom.xml is shown below.

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
        http://maven.apache.org/xsd/maven-4.0.0.xsd">
 
    <modelVersion>4.0.0</modelVersion>
 
    <groupId>com.devkuma</groupId>
    <artifactId>SampleMavenApp</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>
 
    <name>SampleMavenApp</name>
    <url>http://maven.apache.org</url>
 
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
 
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
 
        <!--h2-->
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.196</version>
        </dependency>
 
        <!-- eclipse.persistence-->
        <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>
        <!--resource folder-->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
 
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.6.0</version>
                <configuration>
                    <mainClass>com.devkuma.App</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
 
</project>

This time, only exec-maven-plugin for exec:java is included among the plugins. Since we only need it to work for now, this is enough.

Besides the three <dependency> tags, unfamiliar tags have also been added to the <build> tag. These are all required for using JPA.

What Was Added to pom.xml

Here is a brief summary of what was added to pom.xml this time.

Eclipse Java Persistence API

To use JPA, you need the Java Persistence API (javax.persistence) and a JPA implementation library. This time, we use open source libraries developed by the Eclipse Foundation.

Java Persistence API

<dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>javax.persistence</artifactId>
    <version>2.2.0</version>
</dependency>

This is the library for the javax.persistence package. Without the API, nothing can be done, so it is required.

JPA

<dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>org.eclipse.persistence.jpa</artifactId>
    <version>2.7.0</version>
</dependency>

This is the Eclipse Foundation implementation of JPA. It is also needed to use JPA. Think of these two libraries as a required pair.

Together with the H2 library mentioned earlier, adding these three libraries enables H2 database access through JPA.

Registering the Resource Folder

There is also a newly added part in the <build> tag. It is this tag section.

<resources>
    <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
    </resource>
</resources>

This registers the resource folder. <resource> describes information about a resource folder, and here it adds the src/main/resources path. With this, resource files in the resources folder under main are added to the package loaded during the build.