# JPA N+1 Query Smell Detector

> Static analysis skill to detect and fix JPA N+1 queries, fetch join errors, and Hibernate smells in Spring Boot.

JPA N+1 Query Smell Detector is a free AI agent skill on Agensi by Leo, in APIs & Backend. It loads into your AI agent through the Agensi MCP server, always in its latest approved version.

- Price: Free
- Creator: [Leo](https://www.agensi.io/creators/gin)
- Category: [APIs & Backend](https://www.agensi.io/skills/apis-backend)
- Uses: 1
- Version: 1.0
- Last updated: 2026-10-10
- Security: scan passed (2026-10-10); every new listing is also reviewed by a person
- Listing: https://www.agensi.io/skills/jpa-n-1-query-smell-detector
- Tags: api, java, spring-boot, hibernate, jpa, code-review, n1-problem

## About this skill

Identifying JPA and Hibernate performance bottlenecks during development is difficult without running the application and monitoring logs. This skill performs static analysis on Spring Boot codebases to find N+1 query patterns and JPA smells before they hit production.

### What it does
- **Entity Mapping** catalogs all `@Entity` definitions and identifies potentially problematic LAZY associations.
- **Usage Analysis** tracks where lazy fields are accessed in loops, streams, mappers, or Jackson serialization points.
- **Fetch Strategy Audit** checks if parent queries use `JOIN FETCH`, `@EntityGraph`, or DTO projections to prevent extra roundtrips.
- **Severity Scoring** classifies findings from P0 (critical N+1) to P2 (minor optimization) using a standardized smell reference.
- **Fix Generation** provides a one-sentence remediation and a verification method for every detected issue.

### How it works
1. **Map entities** by scanning for `@Entity` annotations and recording all LAZY relationships.
2. **Trace usage** by searching for those relationships within loops, MapStruct mappers, or controller return types.
3. **Audit queries** at the repository level to see if fetch joins are missing where they are needed.
4. **Generate report** using the standardized P0-P2 format including file paths and line numbers.

### Frameworks & tools
Built for Spring Boot, JPA, and Hibernate. Supports analysis of MapStruct, Jackson serialization, and standard Spring Data JPA repositories.

### Why this beats prompting it yourself
Generic prompts often miss subtle N+1 triggers like Jackson serialization or MultipleBagFetchException risks. This skill uses a structured reference table to ensure every scan is consistent, systematic, and covers edge cases like pagination with fetch joins.

### Use cases
- Scan a new PR for performance regressions before merging to main.
- Audit a legacy Spring Boot service that is experiencing slow database response times.
- Validate that junior developers are following proper JPA fetching strategies in complex domain models.

### Known limitations
Static analysis only. It cannot detect dynamic query issues caused by reflection or runtime bytecode enhancement not visible in source code.

## How to use this skill

1. Connect your AI agent to Agensi once: add the MCP server https://mcp.agensi.io/mcp/auth and sign in with your Agensi account (OAuth, no API key). Steps for Claude, ChatGPT, Claude Code, Codex, Cursor, VS Code and other agents: https://www.agensi.io/docs/mcp-setup
2. Ask your agent: "Use the JPA N+1 Query Smell Detector skill from Agensi" and describe your task.
3. Your agent loads the skill from Agensi when it needs it. Nothing is downloaded.

## Frequently asked questions

### How does this skill differ from standard IDE linting for JPA?

This skill identifies N+1 query patterns by tracing LAZY association access within loops, mappers, and serialization points, allowing you to fix performance bottlenecks during development rather than after a production slowdown.

### Which Java frameworks and libraries are compatible with this detector?

It is specifically designed for Spring Boot applications using JPA/Hibernate and provides out-of-the-box analysis for MapStruct and Jackson serialization risks.

### Does it provide specific instructions on how to fix the detected N+1 queries?

Yes, the skill categorizes findings into P0 to P2 severity levels, providing both a remediation strategy and a verification method for each detected smell.

### Do I need to connect this skill to a running database to perform a scan?

The skill focuses on static analysis of your source code and repository patterns, meaning it does not require a live database connection or runtime profiler to operate.

### What is included in the purchase of this AI agent skill?

Your purchase includes the core JPA analysis logic, a structured smell reference table for consistent reporting, and any future logic updates released for this version.

---

Source: [Agensi](https://www.agensi.io), the marketplace of skills for AI agents. Any MCP-compatible agent (Claude, ChatGPT, Claude Code, Codex, Cursor, VS Code and more) connects at https://mcp.agensi.io/mcp/auth and signs in with an Agensi account; setup for every agent: https://www.agensi.io/docs/mcp-setup. Append .md to any skill, guide or docs address on agensi.io for its markdown version.
