Video in progress

Strong Middle 🐻 Hibernate and JPA

When moving from Hibernate 5 to 6 (Spring Boot 2 → 3), which changes hurt the most?

javax.persistence → jakarta.persistence, the new SQM query parser (HQL is validated more strictly), the rewrite of @Type/@TypeDef and the custom type API, and changes to the defaults for id generators and sequence naming. Result types and the behavior of some native queries differ as well.

📖 Answer

These are the things I ran into when migrating several projects, in order of pain:

1. Package names. javax.persistence.* → jakarta.persistence.*. It's mechanical, but all the libraries need a Jakarta version too: a single old library is enough to stop the app from starting.

2. Custom types. The old signatures of @TypeDef and UserType are gone. For JSON columns, the old version of hypersistence-utils doesn't work. In most cases there is now a standard way:

@JdbcTypeCode(SqlTypes.JSON)
private Map<String, Object> attributes;

3. The HQL parser (SQM). Hibernate 6 validates queries more strictly. Wrong queries that "slipped through" in 5 now fail at startup or on the first call: mismatched types, some forms of implicit joins, the result type of queries without a select. That's a good thing, but if you haven't covered all your @Querys with tests, the errors show up on prod.

4. Id generation. GenerationType.AUTO and sequence names: in 5 there was often a single shared hibernate_sequence, and in 6 it's <entity>_seq per entity. With an existing database, that means a startup error or id collisions. You need to declare the generators explicitly or write a migration.

5. Results. With a fetch join, duplicates of the root entity are now removed automatically; the types from Query.getResultList() and the timezone defaults for how Instant/OffsetDateTime are stored have changed.

This is how I usually migrate: first clean up all the deprecations on Boot 2.7, then move to Boot 3 in a single step, with integration tests using Testcontainers for every repository. Testing with H2 isn't enough here.