jpa-patterns

Explain JPA and Hibernate performance patterns for N+1 queries and lazy loading.

23|7|Updated Feb 22, 2026
One-click install
npx skills add https://github.com/hzy970907/fish-claude-code --skill jpa-patterns-hzy970907
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: jpa-patterns
Source: https://github.com/hzy970907/fish-claude-code/tree/main/skills/jpa-patterns
Command: npx skills add https://github.com/hzy970907/fish-claude-code --skill jpa-patterns-hzy970907

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill helps developers overcome common performance bottlenecks and tricky issues in JPA and Hibernate, such as the N+1 query problem, lazy loading exceptions, and transaction management complexities.

Core Features & Use Cases

  • N+1 Query Detection & Solutions: Identifies and provides fixes for the N+1 query problem using JOIN FETCH, @EntityGraph, and batch fetching.
  • Lazy Loading Management: Explains FetchType.LAZY vs FetchType.EAGER and offers solutions for LazyInitializationException.
  • Transaction Best Practices: Covers transaction propagation, rollback strategies, and common mistakes.
  • Relationship Mapping: Details best practices for OneToMany, ManyToOne, and ManyToMany relationships, including equals/hashCode.
  • Query Optimization: Demonstrates pagination, DTO projections, and bulk operations.
  • Use Case: A developer is experiencing slow database queries and LazyInitializationException errors in their Spring Boot application. They can consult this Skill to understand the root causes and apply the recommended solutions like using JOIN FETCH or DTO projections.

Quick Start

Use the jpa-patterns skill to explain the N+1 problem and how to solve it with JOIN FETCH.

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 Hibernate?

Fix the N+1 query problem in Hibernate by using JOIN FETCH in JPQL, applying @EntityGraph for dynamic fetching, or enabling batch fetching to reduce database round trips.

Why does LazyInitializationException occur in JPA and how can I solve it?

LazyInitializationException occurs in JPA when accessing a lazily loaded entity relationship outside an active transaction. Solve it by fetching data within transactional boundaries or using JOIN FETCH.

What is the best way to optimize JPA queries for read-only data?

Optimize JPA read-only queries by using DTO projections to fetch only necessary columns, applying pagination to limit result sets, and avoiding full entity loading when possible.

How does transaction propagation work in Spring Data JPA?

Transaction propagation in Spring Data JPA determines how existing transactions interact with new operations. It manages rollback strategies and controls execution boundaries to ensure database consistency.

When should I use FetchType.LAZY versus FetchType.EAGER in entity relationships?

Use FetchType.LAZY for large or infrequently accessed entity relationships to improve performance, and reserve FetchType.EAGER only for small, mandatory associations to prevent unnecessary data loading.

Can I use @EntityGraph to override FetchType.LAZY in Spring Boot?

Yes, you can use @EntityGraph in Spring Boot to dynamically override FetchType.LAZY settings, allowing specific queries to eagerly fetch required associations without changing the default entity mapping.