Category Archives: Hibernate

Spring Data JPA 3 to 4 Migration Guide: Query Derivation Changes, the hibernate-processor Coordinate Swap, and Value-Objects

When I upgraded a service from Spring Boot 3 to 4, the framework and web-layer changes got all the attention — but the part that actually cost me an afternoon was the repository layer. Spring Boot 4 pulls in the Spring Data 2025.1 generation (Spring Data JPA 4.0), and that carries three changes that don’t show up in a casual read of the release notes: an annotation-processor coordinate that was renamed out from under you, a complete rewrite of how derived queries are generated, and a much friendlier story for modelling value objects. None of these are loud failures. Two of them compile cleanly and then misbehave, which is the worst kind.

This guide walks the whole repository-layer migration beginner to advanced, on a real Spring Boot 4.1.0 project running Spring Data JPA 4.0, Hibernate 7.1, and Java 25. We start with how to confirm which Spring Data generation you’re actually on, then the hibernate-processor coordinate swap and the silent metamodel failure it causes, then the CriteriaQuery-to-JPQL rewrite of derived queries and its one real behavioural edge, then value objects — records as embeddables and as projections — and finally the removals that will fail your build outright. Every error message below is the real one, not a paraphrase.

Continue reading Spring Data JPA 3 to 4 Migration Guide: Query Derivation Changes, the hibernate-processor Coordinate Swap, and Value-Objects

Hibernate 5 to 6 to 7 Migration Guide: The Breaking Changes and the Silent Ones

I have now taken three codebases through the Hibernate 5 → 6 → 7 path, and the same pattern repeated each time: the compile errors are the easy part, and the silent behaviour changes — query result types, ID generation, fetch semantics — are what reach production. This guide walks the migration in two deliberate hops (5→6, then 6→7), with the exact errors you will see at each stage and how to fix them. Do not attempt 5→7 in one jump; the diagnostics assume you pass through 6.

Continue reading Hibernate 5 to 6 to 7 Migration Guide: The Breaking Changes and the Silent Ones

10 AI Prompts for Hibernate and JPA

Hibernate is powerful and largely invisible when it works — and deeply confusing when it does not. N+1 queries, LazyInitializationException, detached entity exceptions, first-level cache surprises, and migration scripts that silently break the schema are problems that most Java developers encounter repeatedly throughout their careers. I started keeping a list of Hibernate bugs that slipped through review in a large Spring Boot 3 codebase — things that passed all unit tests, passed all integration tests, and only surfaced under production load or after a schema migration. After a dozen entries the pattern was clear: almost all of them were diagnosable by AI if you pasted the right context. The problem was knowing which context to paste. These 10 prompts encode that knowledge — each one specifies exactly what diagnostic data to provide, so the AI can reason about the real problem rather than the surface symptom. AI assistants handle Hibernate well: they know the JPA specification, the Hibernate-specific extensions, the Spring Data abstractions, and the performance implications of every mapping decision.

This post gives you 10 copy-paste AI prompts for Hibernate and JPA development. Each prompt is designed to produce production-quality output — not tutorial-level simplifications — and includes enough context for the AI to reason about the trade-offs specific to your use case.

For foundational reading, see the Hibernate Tutorials series. For complementary AI prompts, see 10 AI Prompts to Review and Improve Java Code Quality.

Continue reading 10 AI Prompts for Hibernate and JPA

Solving NotYetImplementedException with Native Queries in Spring Boot JPA/Hibernate

If you have ever tried to project only a subset of columns from a native SQL query in Spring Boot with Hibernate, you may have encountered this cryptic error: org.hibernate.query.sqm.internal.SqmUtil$NotYetImplementedException or Pure native scalar queries are not yet supported. It is one of the more confusing Hibernate error messages because the fix is not obvious from the stack trace. This post explains exactly what causes it, shows you three clean solutions, and recommends the right approach for each scenario.

Continue reading Solving NotYetImplementedException with Native Queries in Spring Boot JPA/Hibernate

Hibernate 7: get() vs load() – Which One Should You Actually Use?

Have you ever encountered a LazyInitializationException and spent hours debugging why your data wasn’t there? Or noticed your application’s performance dipping because of unnecessary database hits? Choosing between Hibernate get() vs load() is a fundamental decision that every Java developer faces, yet it remains one of the most misunderstood aspects of the Hibernate framework.

In this guide, we break down the mechanics of these two methods in Hibernate 7, explore the Proxy mechanism, and help you decide which tool to pull from your persistence toolbox.

Continue reading Hibernate 7: get() vs load() – Which One Should You Actually Use?

Mastering Hibernate 7: Merging vs. Refreshing Entities for Robust Data Consistency

Are you struggling with the dreaded “detached entity passed to persist” error or finding that your application UI doesn’t reflect the latest database updates? Managing the lifecycle of entities is arguably the most complex part of working with Jakarta Persistence (JPA). In modern high-concurrency environments, understanding Hibernate merging and refreshing is critical for maintaining data integrity and preventing silent data loss.

In this guide, we will explore how Hibernate 7 handles state transitions, the architectural nuances of the Persistence Context, and exactly when to deploy merge() versus refresh() to keep your application state in sync with your source of truth.

Continue reading Mastering Hibernate 7: Merging vs. Refreshing Entities for Robust Data Consistency

Mastering Hibernate 7: The Ultimate Guide to Inserting Objects Efficiently

Are you struggling with redundant JDBC code or hitting performance bottlenecks when saving data in your Java applications? You aren’t alone. Manually handling SQL INSERT statements, managing database connections, and mapping results to objects is error-prone and time-consuming. Hibernate 7 remains the industry standard for Object-Relational Mapping (ORM), significantly reducing the overhead required to bridge business logic and your relational database.

In this guide, we will dive deep into the most efficient ways to insert objects using Hibernate 7 features, ensuring your persistence layer is both robust and high-performing.

Continue reading Mastering Hibernate 7: The Ultimate Guide to Inserting Objects Efficiently