DEV Community

Cover image for Working with Hibernate in Java - Part 1: Using xml config
Mayank Pant
Mayank Pant

Posted on

Working with Hibernate in Java - Part 1: Using xml config

We don’t want to use Spring because, at the moment, we are trying to learn Hibernate, which is a framework itself. So we just want to focus on one framework at this point, and that is Hibernate.

Creating a simple project

First, we create the most basic Maven project

Sample project


Adding dependency

Adding hibernate-core dependency

<dependency> <groupId>org.hibernate.orm</groupId> <artifactId>hibernate-core</artifactId> <version>7.1.8.Final</version> </dependency> 
Enter fullscreen mode Exit fullscreen mode

We also need to connect to the database, for that we need a vendor, and in this case we are using MySQL. So we need the mysql-connector dependency.

<dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <version>9.5.0</version> </dependency> 
Enter fullscreen mode Exit fullscreen mode

Connecting to the database

We need to connect to the database now. There are 2 ways to do this:

  1. Using xml file: In resources, we create a folder called META-INF and place a persistence.xml file that tells our app where the database is and how to connect to it. We don’t need to remember this file; we can just copy and paste it from the docs.
  2. Programatically: The configuration that we do in xml, can easily be done in a class form

As part of this tutorial we will stick with using xml config file. You can copy-paste this one:

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.2" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd"> <!-- Define persistence unit --> <persistence-unit name="my-persistence-unit" transaction-type="RESOURCE_LOCAL"> <description>description</description> <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> <exclude-unlisted-classes>false</exclude-unlisted-classes> <properties> <!-- database connection --> <property name="jakarta.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver" /> <!-- Ends with your database table name--> <property name="jakarta.persistence.jdbc.url" value="jdbc:mysql://localhost/jpa_2025_ch1" /> <property name="jakarta.persistence.jdbc.user" value="root" /> <property name="jakarta.persistence.jdbc.password" value="toor" /> </properties> </persistence-unit> </persistence> 
Enter fullscreen mode Exit fullscreen mode

Coding Part

We use EntityManager, which is sort of like the manager for context. And this can be used to control the context and the transaction that we are doing.

EntityManager em = null; try { em.getTransaction().begin(); /* * What you want to persist goes here. */ em.getTransaction().commit(); } finally { em.close(); } 
Enter fullscreen mode Exit fullscreen mode

How to get the EntityManager?
It uses the Factory pattern, so we need EntityManagerFactory to create one.

How to get EntityManagerFactory?
We can use the Persistence class method createEntityManagerFactory() and instruct it to use the persistence.xml file's persistence-unit-name

EntityManagerFactory emf = Persistence.createEntityManagerFactory("my-persistence-unit");

Now we have successfully connected to the database.


Describing Entity

Next, we need to describe our Entity. Why?
Because our database can have many tables, and not all are considered Entity. We can have many-to-many mapping and the intermediate table might not be an entity.

For this, we need to describe in our Java code which tables are considered as entity.
And then we can describe separately the relationships between these entities.

  • We create a new package called entities
  • Create a class, lets say Product
  • Annotate it with @Entity to let hibernate know that this represents a table in database.
  • We then provide all the fields in this class, and these fields relate to columns in that table.
@Entity public class Product { @Id private Long id; private String name; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } 
Enter fullscreen mode Exit fullscreen mode
  • We then create an object for this in our class and tell the entity manager to persist it.
EntityManager em = null; try { em.getTransaction().begin(); Product p = new Product(); p.setId(3); p.setName("Apple"); em.persist(p); // add this to the context --> NOT AN INSERT QUERY em.getTransaction().commit(); } finally { em.close(); } 
Enter fullscreen mode Exit fullscreen mode

This persist does not mean that it will be an INSERT operation on the database. That is the philosophy of JDBC. In JPA, this can or cannot be an insert, all depends on the context.

Think of this like, context managing all your queries, and it keeps track of the operation you do on objects. It then, at the end when the transaction is to be committed, will mirror the context onto the database and if there are differences then that will be committed to the database.

Top comments (0)