Single logo
  • Courses
  • Blog
  • About
Join on SkoolJoin on Skool

In this topic

Hibernate
  • SQLite and a JDBC Connection Pool in a Java Web App
  • Why an ORM: The Same UserDao in JDBC and in Hibernate
  • Hibernate 7 Setup: Three Dependencies, hibernate.cfg.xml and a First Session
  • Hibernate Entity Classes and CRUD With the Session: persist, find, a Setter and remove
  • Hibernate Inside a Servlet Web App: Replace the JDBC DAO, Keep Every Page
  • Spring MVC and SQLite Without Spring Boot: the Mini Blog in the Browser
  • Hibernate Entity Classes and CRUD With the Session: persist, find, a Setter and remove
  • Entities and H2 in Spring Boot: a Post Class Becomes a Table
  • JPA Entity Mapping: Ids, Enums and Dates, From a Java Class to a Table
  • HQL in Hibernate 7: List, Where With a Named Parameter, Bulk Update and Bulk Delete
  • The N+1 Problem in JPA: Fetch Types, Join Fetch and Entity Graphs
  1. Blog
  2. /
  3. Hibernate
  4. /
  5. Fundamentals

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.

Chaand Sheikh

·27 November 2026·Updated 28 November 2026Fundamentals

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

pom.xml downloads Hibernate. hibernate.cfg.xml says which database. The SessionFactory is built once per app. A Session is opened for each piece of work and closed after it. pom.xmldownloads Hibernate hibernate.cfg.xmlwhich database SessionFactoryonce per app Sessionone per piece of work Like a shop: open it once in the morning, then serve one customer at a time.
Build the SessionFactory once; open, use and close a Session for each operation.

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-core is Hibernate itself. The version here is 7.3.13.Final; yours may be newer, and that's fine.
  • sqlite-jdbc is the JDBC driver, the piece that actually talks to the database. Move to Postgres and this is what you change.
  • hibernate-community-dialects holds 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 with no 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.

Keep going in Hibernate · 3 of 11

Previous
Why an ORM: The Same UserDao in JDBC and in Hibernate
Next post
Hibernate Entity Classes and CRUD With the Session: persist, find, a Setter and remove
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

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

Join free on Skool

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.

On this page

  • Four pieces, one job each
  • Step 1: three dependencies
  • Step 2: hibernate.cfg.xml
  • Step 3: connect, ask, print
  • Common mistakes
  • Quick recap
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

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

Join free on Skool

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)

Single logo

Empowering Education, One Course at a Time

About
About usContact us
Legal
Terms of UsePrivacy Policy

© 2026 StudyEasy Organization. All rights reserved.