Hibernate 7 Setup: Three Dependencies, hibernate.cfg.xml and a First Session
Four pieces, one job each: three Maven dependencies, a two-property hibernate.cfg.xml, one SessionFactory per app and one Session per piece of work. A small main method proves it: Rows in users: 3.
What you will learn
- Add hibernate-core, a JDBC driver and the SQLite dialect to pom.xml
- Point hibernate.cfg.xml at the database and turn on show_sql
- Build one SessionFactory and open a Session for one piece of work
- Run a first query to prove the database answers
Before you start
- Why an ORM: The Same UserDao in JDBC and in Hibernate
- Maven in IntelliJ: pom.xml and dependencies
The Hibernate setup is short. There are three dependencies, one configuration file, and one small program that reaches the database.
Four pieces, one job each
Step 1: three dependencies
<!-- Hibernate 7.3: the ORM, with Jakarta Persistence 3.2 -->
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>7.3.13.Final</version>
</dependency>
<!-- the JDBC driver Hibernate uses to talk to SQLite -->
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.53.4.0</version>
</dependency>
<!-- the SQLite dialect lives in the community dialects -->
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-community-dialects</artifactId>
<version>7.3.13.Final</version>
</dependency>
hibernate-coreis Hibernate itself. The version here is 7.3.13.Final; yours may be newer, and that's fine.sqlite-jdbcis the JDBC driver, the piece that actually talks to the database. Move to Postgres and this is what you change.hibernate-community-dialectsholds the SQLite dialect. A dialect is how Hibernate shapes its SQL for one database, because each database writes SQL a little differently.
Step 2: hibernate.cfg.xml
This file sits in src/main/resources and tells the session factory about your database:
<?xml version="1.0" encoding="UTF-8"?>
<hibernate-configuration xmlns="http://www.hibernate.org/xsd/orm/cfg">
<session-factory>
<!-- the database: one file in your home folder -->
<property name="hibernate.connection.url">jdbc:sqlite:${user.home}/studyeasy.db</property>
<!-- print every SQL statement Hibernate sends -->
<property name="hibernate.show_sql">true</property>
</session-factory>
</hibernate-configuration>
One session-factory block, two properties. hibernate.connection.url says where the database is; for SQLite that's one file, studyeasy.db in your home folder. A database with a username and password would get those properties here too. hibernate.show_sql set to true prints every SQL statement Hibernate sends, which helps a lot while debugging.
Step 3: connect, ask, print
This is a plain class with a main method, not a web app:
package org.studyeasy.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Main {
public static void main(String[] args) {
// configure() reads src/main/resources/hibernate.cfg.xml
SessionFactory factory =
new Configuration().configure().buildSessionFactory();
Session session = factory.openSession();
// plain SQL for now: it only proves the database answers
String version = session.createNativeQuery(
"select sqlite_version()", String.class).getSingleResult();
System.out.println("Connected to: SQLite " + version);
Long users = session.createNativeQuery(
"select count(*) from users", Long.class).getSingleResult();
System.out.println("Rows in users: " + users);
session.close();
factory.close();
}
}
new Configuration().configure() reads the XML file, and buildSessionFactory() returns the factory. openSession() gives the session you run queries with. At the end, both are closed. The queries here are plain SQL on purpose; the only goal is to see the database answer. Run it with the green arrow next to main:
Hibernate: select sqlite_version()
Connected to: SQLite 3.53.4
Hibernate: select count(*) from users
Rows in users: 3
The lines starting with Hibernate: come from show_sql. Above them, IntelliJ also shows a lot of red startup log; that's Hibernate starting up on stderr, not an error.
Common mistakes
- A typo in the database file name, say
studyeasy2.db. SQLite quietly creates that file, empty, and the count fails withno such table: users. - Building a new SessionFactory for every operation. It's slow to build; make one per app.
- Forgetting the dialect dependency for SQLite. Hibernate's core doesn't include it.
Quick recap
Add the dependencies, configure hibernate.cfg.xml, build the factory, open a session. One SessionFactory for the app, one Session per operation. Next, map a real class and do CRUD in entity classes and CRUD with the Session.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE
Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)Related posts
SQLite and a JDBC Connection Pool in a Java Web App
A whole database in one file, studyeasy.db, and a Java web app that talks to it: one HikariCP pool, a @WebListener that opens it and runs schema.sql at startup, and a /db-check servlet that borrows a connection.
Why an ORM: The Same UserDao in JDBC and in Hibernate
One UserDao, written twice. The JDBC version spells out every SQL string and copies every column; the Hibernate version persists, finds and merges objects and lets Hibernate write the SQL. 66 lines against 40, and what that costs.
Hibernate Entity Classes and CRUD With the Session: persist, find, a Setter and remove
Four small programs against a three-row users table: persist adds Noah and the id comes back as 4, find reads Emma (twice, with one SELECT), a setter changes Liam's email, and remove deletes Noah. Every change goes out at commit.
Hibernate Inside a Servlet Web App: Replace the JDBC DAO, Keep Every Page
The Users web app's UserDao, rewritten with Hibernate: same five methods, no SQL strings, one SessionFactory built on the existing HikariCP pool, and zero of the eight servlets and JSPs changed.
