Hibernate
Hibernate ORM: entities, sessions, mappings and queries.
Start here
Fundamentals
- 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 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.
- 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.
- Spring MVC and SQLite Without Spring Boot: the Mini Blog in the Browser
Five small classes put the Mini Blog in the browser: BlogServer starts Tomcat with a DispatcherServlet, BlogConfig turns on MVC and builds a SessionFactory for blog.db, PostController answers /posts and PostRepository reads with Hibernate. Run it with docker compose.
Mappings
- 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.
- Entities and H2 in Spring Boot: a Post Class Becomes a Table
The blog's cards are typed into HTML. A Post class with @Entity and @Id becomes the POST table in an in-memory H2 database: three pom lines, a few YAML settings, and Hibernate writes the create table SQL at startup.
- JPA Entity Mapping: Ids, Enums and Dates, From a Java Class to a Table
A Book class becomes a book table: every annotation and the column it produces, IDENTITY versus SEQUENCE ids, the ORDINAL enum trap that turns The Hobbit into a mystery, and LocalDate versus Instant.
Queries
- HQL in Hibernate 7: List, Where With a Named Parameter, Bulk Update and Bulk Delete
HQL talks about classes, not tables. List every User, find Olivia with :email, move two users to a new email domain in one bulk update, then delete everyone after id 1, with the SQL Hibernate printed for each.
- The N+1 Problem in JPA: Fetch Types, Join Fetch and Entity Graphs
Relay's ticket list of 50 tickets quietly sends 51 queries. Count them with Hibernate statistics, bring it down to 1 with a left join fetch or an entity graph, and see why EAGER still sends 51.
