java-jpa-patterns

Diagnose and fix JPA/Hibernate performance issues including N+1 queries, lazy loading, and transaction pitfalls.

Updated Apr 20, 2020
One-click install
npx skills add https://github.com/UnterrainerInformatik/java-rdb-utils --skill java-jpa-patterns-unterrainerinformatik
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: java-jpa-patterns
Source: https://github.com/UnterrainerInformatik/java-rdb-utils/tree/main/.agents/skills/java-jpa-patterns
Command: npx skills add https://github.com/UnterrainerInformatik/java-rdb-utils --skill java-jpa-patterns-unterrainerinformatik

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 misconfigured transactions that are hard to diagnose without deep framework knowledge. ## Core Features & Use Cases - N+1 Query Detection and Fixes: Apply JOIN FETCH, @EntityGraph, or @BatchSize to eliminate excessive SELECT statements. - Lazy Loading Guidance: Resolve LazyInitializationException with DTO projections, transactional boundaries, or fetch strategies. - Transaction and Relationship Patterns: Correct @Transactional usage, propagation settings, bidirectional relationship sync, and optimistic locking with @Version. - Use Case: When a developer sees 100 SQL queries while loading 10 orders, this Skill identifies the N+1 problem and provides the exact JOIN FETCH or @EntityGraph solution. ## Quick Start Ask the assistant to review your JPA repository or entity code for N+1 query problems and lazy loading issues.

Frequently Asked Questions about java-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 using JOIN FETCH in JPQL queries, @EntityGraph on repository methods, or Hibernate's @BatchSize annotation. Enable SQL logging with spring.jpa.show-sql to detect the problem first.

How to resolve LazyInitializationException in Spring Boot?▼

LazyInitializationException occurs when accessing lazy-loaded associations outside a transaction. Resolve it with JOIN FETCH queries, @Transactional service methods, DTO projections, or enabling spring.jpa.open-in-view.

Should I use EAGER or LAZY fetch type in JPA?▼

Use FetchType.LAZY by default, especially on @ManyToOne and @OneToOne which default to EAGER. Fetch associations explicitly with JOIN FETCH or @EntityGraph only when the use case requires them.

Why is @Transactional ignored when calling a method in the same class?▼

Spring transactions use proxies, so internal self-invocation bypasses the proxy and the @Transactional annotation is ignored. Inject the bean into itself or move the transactional method to a separate service.

When should I use optimistic locking with @Version?▼

Use @Version on entities accessed concurrently by multiple users to prevent lost updates. When a conflict occurs, Hibernate throws OptimisticLockException, which you can catch and retry or report to the user.