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
Pageablefor collections. - •Open-In-View: Set
spring.jpa.open-in-view=falseto detect lazy loading issues early.
Query Optimization
- •N+1 Problem: Use
JOIN FETCH(JPQL) or@EntityGraphfor relationships. - •Bulk Operations: Use
@Modifyingfor 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.