Philipp Frauenthaler & Gregor Lucny

Hibernate: How to keep calm

Stop letting Hibernate generate your database schema. Learn a more robust approach, plus how to fix the N+1 problem and stream large datasets without memory issues.

Hibernate: How to keep calm
#1about 6 minutes

Why you should use an ORM like Hibernate

Using an object-relational mapper (ORM) like Hibernate simplifies database access in Java by handling boilerplate code, type conversions, and transaction management that is complex with plain JDBC.

#2about 3 minutes

Mapping Java classes and the entity lifecycle

Entities move through transient, managed, and detached states, where changes to managed entities are automatically persisted to the database upon transaction commit.

#3about 2 minutes

Understanding Hibernate's caching layers

Hibernate uses a mandatory Level 1 cache scoped to the transaction to reduce database roundtrips, with an optional Level 2 cache for data shared across transactions.

#4about 3 minutes

Integrating Hibernate with Spring and Spring Data JPA

Spring Boot simplifies Hibernate configuration and transaction management, while Spring Data JPA further abstracts data access by generating repository implementations from interfaces.

#5about 3 minutes

Keeping your database schema in sync with Liquibase

Use a database migration tool like Liquibase and its diff plugin to automatically generate and apply schema changes based on your JPA entities, ensuring consistency.

#6about 3 minutes

Choosing an effective ID generation strategy

Client-generated UUIDs are often preferable to database auto-incrementing IDs because they are available before persistence and simplify data copying between environments.

#7about 3 minutes

Implementing equals and hashCode for JPA entities

Avoid using all properties in `equals` and `hashCode` for entities due to lazy loading and lifecycle issues; instead, base equality on the stable primary key.

#8about 5 minutes

Solving the N+1 query problem with join fetch

Prevent the N+1 query problem caused by lazy loading by using `JOIN FETCH` in your JPQL query to load related entities in a single database trip.

#9about 4 minutes

Processing large datasets with streaming and detaching

Handle large result sets efficiently by streaming data from the database and detaching processed entities from the session to prevent high memory consumption.

#10about 3 minutes

Managing transactions with external systems

Ensure data consistency with non-transactional systems like REST APIs by registering custom rollback actions using Spring's `TransactionSynchronizationManager`.

#11about 2 minutes

Automating actions with Hibernate entity listeners

Use Hibernate's entity listener hooks like `@PostPersist` and `@PostUpdate` to automatically trigger actions, such as sending messages to a message broker, when an entity changes.

#12about 16 minutes

Key takeaways for using Hibernate effectively

While Hibernate improves development efficiency, it is crucial to analyze the generated SQL, understand its internal workings, and recognize when it may not be the right tool for the job.

Related jobs
Jobs that call for the skills explored in this talk.

Software Engineer

tree-IT GmbH
Bad Neustadt an der Saale, Germany

Intermediate
Senior

Featured Partners

Related Articles

View all articles
BB
Benedikt Bischof
Using Java 17 latest features in real world projects
Welcome to this issue of the WeAreDevelopers Live Talk series. This article recaps an interesting talk by Ron Veen who introduced the audience to some of the most interesting features that got added to the Java language.About the speaker:Ron is a hig...
Using Java 17 latest features in real world projects
CH
Chris Heilmann
WeAreDevelopers LIVE days are changing - get ready to take part
Starting with this week's Web Dev Day edition of WeAreDevelopers LIVE Days, we changed the the way we run these online conferences. The main differences are:Shorter talks (half an hour tops)More interaction in Q&AA tips and tricks "Did you know" sect...
WeAreDevelopers LIVE days are changing - get ready to take part
CH
Chris Heilmann
With AIs wide open - WeAreDevelopers at All Things Open 2025
Last week our VP of Developer Relations, Chris Heilmann, flew to Raleigh, North Carolina to present at All Things Open . An excellent event he had spoken at a few times in the past and this being the “Lucky 13” edition, he didn’t hesitate to come and...
With AIs wide open - WeAreDevelopers at All Things Open 2025
BR
Benjamin Ruschin
What Developers Really Need to Create Great Code Demos
Every developer on earth has, at some point, had another developer to thank for a breakthrough, a success, an aha moment they wouldn’t have had without coming across that blog post, that open-source contribution, that reply on socials or that humble ...
What Developers Really Need to Create Great Code Demos

From learning to earning

Jobs that call for the skills explored in this talk.