home / skills / partme-ai / full-stack-skills / spring-data-jpa

spring-data-jpa skill

/skills/spring-data-jpa

This skill provides guidance for Spring Data JPA repositories, entity management, and queries to help build robust data access layers.

npx playbooks add skill partme-ai/full-stack-skills --skill spring-data-jpa

Review the files below or copy the command above to add this skill to your agents.

Files (2)
SKILL.md
693 B
---
name: spring-data-jpa
description: Provides comprehensive guidance for Spring Data JPA including repositories, entity management, query methods, and database operations. Use when the user asks about Spring Data JPA, needs to work with JPA repositories, implement data access layers, or configure JPA in Spring.
license: Complete terms in LICENSE.txt
---

## When to use this skill

Use this skill whenever the user wants to:
- [待完善:根据具体工具添加使用场景]

## How to use this skill

[待完善:根据具体工具添加使用指南]

## Best Practices

[待完善:根据具体工具添加最佳实践]

## Keywords

[待完善:根据具体工具添加关键词]

Overview

This skill provides practical, hands-on guidance for Spring Data JPA, covering repository design, entity mapping, query methods, transactions, pagination, and performance tuning. It helps developers implement robust data access layers, configure JPA in Spring Boot, and resolve common pitfalls. The guidance is focused on actionable patterns, code examples, and configuration tips for real-world applications.

How this skill works

The skill inspects user questions about JPA repositories, entity relationships, query generation, and configuration and returns targeted recommendations and example code snippets. It explains when to use derived queries, @Query annotations, Specification/Criteria API, and custom repository implementations. It also addresses transaction boundaries, lazy vs eager fetching, and strategies for optimizing SQL and preventing common ORM issues.

When to use it

  • Designing or refactoring data access layers using Spring Data JPA
  • Defining repository interfaces, custom queries, and projection DTOs
  • Configuring JPA, Hibernate properties, and database dialects in Spring Boot
  • Troubleshooting lazy loading, N+1 queries, or transaction problems
  • Implementing pagination, sorting, and bulk update/delete operations

Best practices

  • Prefer repository interfaces and derive simple queries; use @Query for complex joins
  • Model entities with clear ownership and explicit fetch strategies to avoid N+1 problems
  • Keep transaction boundaries at the service layer and mark read-only where appropriate
  • Use DTO projections or constructor expressions to avoid fetching unnecessary columns
  • Employ optimistic locking for concurrent updates and database-level indexes for query speed

Example use cases

  • Create a UserRepository with derived finder methods and pageable search
  • Write a @Query with JOIN FETCH to load a parent and children while avoiding multiple selects
  • Implement a Specification-based dynamic filter API for complex search forms
  • Configure Spring Boot properties for Hibernate SQL logging, naming strategy, and ddl-auto
  • Resolve a LazyInitializationException by adjusting fetch strategy or opening a transaction

FAQ

When should I use a custom repository implementation?

Use custom implementations for complex persistence logic, batch operations, or when you need access to EntityManager beyond what derived queries provide.

How do I avoid N+1 queries?

Use JOIN FETCH in JPQL or entity graphs, adjust fetch types, or use batch fetching and careful DTO projection to limit queries.