Set Up a Barebones Java project with JPA

JPA with Hibernate (7 Part Series)

1 Getting Started With Hibernate And JPA
2 Set Up a Barebones Java project with JPA
3 more parts…
3 Simplifying Database Operations with JPA and Hibernate: Creating and Persisting Objects
4 Know The JPA Persistence.xml File
5 Persisting non-primitive types in JPA
6 JPA: One-To-One Relationship, Fetch type, Relationship Direction, And Query Optimization
7 JPA : One-To-Many, Many-To-One And Many-To-Many Relationships

Typically JPA is integrated with Spring/Spring Boot and Java EE/Jakarta EE applications. Both these applications provide an abstraction over the JPA which hides a lot of stuff that we need to do manually in a barebones Java application. This abstraction over JPA works like magic. But here we are going to do the magic ourselves.

Setting up a database

First, we need to set up a database for our application. For the sake of simplicity and development purposes, we are going to use the H2 database. H2 can be used as an in-memory database or serve mode. We’ll use server-side mode to simulate a real database.

We can switch the H2 database with any other relational database like MySQL, Postgres, or whatever else as long as it is relational.

Download and install the H2 setup from the official site. Once installed, if you open the H2 console it opens up in the browser. You should see something like this.

图片[1]-Set Up a Barebones Java project with JPA - 拾光赋-拾光赋
You set your username and password or can click the connect button it will run the H2 Database with default config.

You should see a page like this once you are connected. You can create tables and write SQL queries here.

Creating a Java Maven Project

If you are using IntelliJ Idea, create a new Java maven project. Once the project build completes add the following dependency in the pom.xml file

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-entitymanager</artifactId>
  <version>5.6.15.Final</version>
</dependency>`

<dependency>
  <groupId>com.h2database</groupId>
  <artifactId>h2</artifactId>
  <version>1.4.200</version>
  <scope>complete</scope>
</dependency>

Enter fullscreen mode Exit fullscreen mode

The first dependency allows us to work with hibernate implementation which supports JPA and the second is for the database driver. In our case, it is an H2 database. If you are working with any other database, just switch the dependency with the respective database driver.

Persistence Context

In the src → resources package create META-INF directory. In that directory create a persistence.xml file. This file is called the persistence context.

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
             version="2.0">
    <persistence-unit name="myPersistenceUnit" transaction-type="RESOURCE_LOCAL">
        <properties>
            <!-- Database connection properties -->
            <property name="javax.persistence.jdbc.url" value="jdbc:h2:tcp://localhost/~/test"/>
            <property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
            <property name="javax.persistence.jdbc.user" value="sa"/>
            <property name="javax.persistence.jdbc.password" value=""/>

            <!-- Hibernate properties -->
            <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
            <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
        </properties>
    </persistence-unit>
</persistence>

Enter fullscreen mode Exit fullscreen mode

It contains the configuration for our database. How to connect, where to connect, username, password, and other configurations. If you have experience with spring boot it is similar to the application.properties file.

We’ll look in more detail at the persistence context file. For now, our bare-bones Java project with JPA is ready to run.


Follow me on : Twitter, LinkedIn, GitHub, Linktree

JPA with Hibernate (7 Part Series)

1 Getting Started With Hibernate And JPA
2 Set Up a Barebones Java project with JPA
3 more parts…
3 Simplifying Database Operations with JPA and Hibernate: Creating and Persisting Objects
4 Know The JPA Persistence.xml File
5 Persisting non-primitive types in JPA
6 JPA: One-To-One Relationship, Fetch type, Relationship Direction, And Query Optimization
7 JPA : One-To-Many, Many-To-One And Many-To-Many Relationships

原文链接:Set Up a Barebones Java project with JPA

© 版权声明
THE END
喜欢就支持一下吧
点赞9 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容