jpa-patterns

Diagnose and resolve JPA/Hibernate performance issues including N+1 queries and lazy loading errors.

Updated Dec 23, 2025
One-click install
npx skills add https://github.com/zuldare/apuntesIA --skill jpa-patterns-zuldare
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: jpa-patterns
Source: https://github.com/zuldare/apuntesIA/tree/main/skills/jpa-patterns
Command: npx skills add https://github.com/zuldare/apuntesIA --skill jpa-patterns-zuldare

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? JPA and Hibernate applications frequently suffer from hidden performance problems like N+1 queries, LazyInitializationException errors, and inefficient fetch strategies that are hard to diagnose without deep framework knowledge. ## Core Features & Use Cases - N+1 Query Resolution: Fix the most common JPA performance killer using JOIN FETCH, @EntityGraph, or batch fetching with @BatchSize. - Lazy Loading Guidance: Resolve LazyInitializationException with proper transaction boundaries, DTO projections, and fetch strategies. - Fetch Strategy Best Practices: Configure LAZY vs EAGER loading correctly and detect problems with SQL logging. - Use Case: When a Spring Boot endpoint suddenly executes hundreds of SELECT statements, use this Skill to identify the N+1 pattern and rewrite the repository query with JOIN FETCH or @EntityGraph. ## Quick Start Ask the AI to diagnose why your Spring Data repository triggers dozens of extra queries when loading an entity with its relationships.

Frequently Asked Questions about jpa-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I fix the N+1 query problem in JPA?▼

Fix N+1 queries in JPA by using JOIN FETCH in a JPQL query, applying @EntityGraph on repository methods, or enabling batch fetching with @BatchSize. Each approach loads related entities in one or few queries instead of one query per parent row.

How to resolve LazyInitializationException in Spring Boot?▼

LazyInitializationException occurs when a lazy association is accessed outside an active transaction. Resolve it by fetching the association with JOIN FETCH in the query, keeping the service method @Transactional, or mapping to a DTO inside the transaction.

Should I use EAGER or LAZY fetching in JPA entities?▼

Use LAZY fetching for all associations, including @ManyToOne and @OneToOne which default to EAGER. EAGER loading fetches data whether needed or not, causing unnecessary joins and N+1-style overhead; fetch explicitly with JOIN FETCH when required.

What is the difference between JOIN FETCH and @EntityGraph?▼

JOIN FETCH is written directly in a JPQL @Query for explicit control over the SQL, while @EntityGraph is a declarative annotation on repository methods specifying attribute paths to fetch. Both produce a single query that loads associations eagerly for that call.

How do I detect N+1 queries in a Hibernate application?▼

Detect N+1 queries by enabling SQL logging: set spring.jpa.show-sql to true and logging levels for org.hibernate.SQL to DEBUG. Count the SELECT statements per request; more than a few for a single operation indicates an N+1 problem.

When should I use @BatchSize instead of JOIN FETCH?▼

Use @BatchSize when fetching multiple collections or when JOIN FETCH would cause a cartesian product across several associations. It loads lazy collections in batches of the configured size, reducing query count without changing query structure.