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
Adding dependency
Adding hibernate-core dependency
<dependency> <groupId>org.hibernate.orm</groupId> <artifactId>hibernate-core</artifactId> <version>7.1.8.Final</version> </dependency> 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> Connecting to the database
We need to connect to the database now. There are 2 ways to do this:
- Using xml file: In resources, we create a folder called META-INF and place a
persistence.xmlfile 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. - 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> 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(); } 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
@Entityto 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; } } - 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(); } 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)