home / skills / hoangnguyen0403 / agent-skills-standard / data-access
This skill helps you implement Spring Boot data access best practices for JPA and Hibernate, improving performance and reliability.
npx playbooks add skill hoangnguyen0403/agent-skills-standard --skill data-accessReview the files below or copy the command above to add this skill to your agents.
---
name: Spring Boot Data Access
description: Best practices for JPA, Hibernate, and Database interactions in Spring Boot
metadata:
labels: [spring-boot, jpa, hibernate, database]
triggers:
files: ['**/*Repository.java', '**/*Entity.java']
keywords: [jpa-repository, entity-graph, transactional, n-plus-1]
---
# Spring Boot Data Access
## **Priority: P0**
## Implementation Guidelines
### JPA & Hibernate
- **Read-Only**: Default to `@Transactional(readOnly = true)` on Services. Override only on write methods.
- **Projections**: Use Java Records for read-only queries. Avoid fetching full Entities.
- **Pagination**: ALWAYS use `Pageable` for collections.
- **Open-In-View**: Set `spring.jpa.open-in-view=false` to detect lazy loading issues early.
### Query Optimization
- **N+1 Problem**: Use `JOIN FETCH` (JPQL) or `@EntityGraph` for relationships.
- **Bulk Operations**: Use `@Modifying` for batch updates/deletes to bypass Entity overhead.
## Anti-Patterns
- **N+1 Selects**: `**No loops in loops**: Fetch eagerly or use graphs.`
- **Logic in Queries**: `**No complex JPQL**: Use Service logic.`
- **Returning Streams**: `**No raw Streams**: Use Lists or Page.`
## References
- [Implementation Examples](references/implementation.md)
This skill captures best practices for JPA, Hibernate, and database interactions in Spring Boot. It focuses on safe defaults, query optimization, and avoiding common performance anti-patterns. The goal is to help agents generate code that is maintainable, performant, and predictable in production.
The skill inspects service and repository patterns and recommends transactional defaults, projection strategies, and pagination usage. It highlights query-level fixes for N+1 select issues and suggests bulk operation techniques that avoid entity-management overhead. It also enforces configuration recommendations to reveal lazy-loading problems early.
Why set @Transactional(readOnly = true) by default?
Read-only transactions signal intent, can enable optimizations, and reduce accidental writes. Mark specific methods as write transactions when needed.
When should I use JOIN FETCH vs @EntityGraph?
JOIN FETCH is explicit in JPQL and good for single custom queries; @EntityGraph is reusable and declarative for repository methods and avoids query string duplication.