spring-data-jpa

Implements Spring Data JPA and Hibernate persistence patterns for Java 21+ relational databases.

39|3|Updated Jul 28, 2025
One-click install
npx skills add https://github.com/mzivkovicdev/spring-crud-generator --skill spring-data-jpa-mzivkovicdev
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: spring-data-jpa
Source: https://github.com/mzivkovicdev/spring-crud-generator/tree/main/.agents/skills/spring-data-jpa
Command: npx skills add https://github.com/mzivkovicdev/spring-crud-generator --skill spring-data-jpa-mzivkovicdev

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Designing correct, performant JPA persistence is hard: N+1 queries, lost updates, unbounded lock waits, and misconfigured transactions silently corrupt data or exhaust connection pools in production. This Skill encodes verified Spring Data JPA and Hibernate rules so entities, repositories, transactions, and locking behave predictably on any relational database. ## Core Features & Use Cases - Entity and association mapping: Enforces correct entity equality strategies, aggregate-scoped associations, LAZY to-one mappings, and alignment between mappings, migrations, and constraints. - Concurrency and locking: Provides per-operation strategy selection across optimistic locking with a composed retry annotation, pessimistic locks with engine-specific timeouts, and atomic conditional UPDATE statements. - Query and resource governance: Covers fetch plans, projections, pagination, sargable dynamic queries, statement and transaction timeouts, and connection pool sizing. - Use Case: When adding a stock-reservation feature, apply this Skill to choose a pessimistic lock or conditional UPDATE, bound the lock wait per database engine, and write concurrency tests that prove the conflict contract. ## Quick Start Use the spring-data-jpa skill to review my JPA entity mappings and repository queries for N+1 problems and correct optimistic locking.

Frequently Asked Questions about spring-data-jpa

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I fix N+1 queries in Spring Data JPA?▼

Choose the smallest suitable fetch mechanism: a projection for read-only subsets, @EntityGraph for known graphs, a fetch join for controlled shapes, or batch fetching for intentional lazy traversal. Never use blanket EAGER fetching, and disable Open EntityManager in View with spring.jpa.open-in-view=false.

How to implement optimistic locking with retry in Spring Boot?▼

Add a @Version field to the entity and compose a project-owned @OptimisticLockingRetry annotation combining @Transactional with declarative retry. On Spring Boot 3 use Spring Retry's @Retryable; on Spring Boot 4 use the framework's own resilience support, noting maxRetries counts retries, not attempts.

Does jakarta.persistence.lock.timeout work on PostgreSQL?▼

No, PostgreSQL ignores a positive jakarta.persistence.lock.timeout hint; the wait is bounded only by lock_timeout set at the connection level or per transaction. Oracle honors the hint as for update wait n, and MySQL uses innodb_lock_wait_timeout.

When should I use pessimistic locking instead of optimistic locking?▼

Use a pessimistic lock when the invariant requires blocking: allocating limited stock or seats, claiming a work item, or protecting an invariant spanning rows. Prefer one atomic conditional UPDATE when the whole rule fits one row and one WHERE clause.

Why should equals and hashCode not use the entity identifier directly?▼

The surrogate identifier is null before persistence and assigned after, so an identifier-derived hashCode changes while the instance sits in a hash-based collection. Compare on the identifier with instanceof but return a constant class-derived hashCode, or use a stable natural key when one exists.

Can I use H2 for testing JPA persistence behavior?▼

H2-only tests are not evidence of production behavior. Use Testcontainers or an equivalent environment running the actual supported database engine to test mappings, constraints, locking, bulk DML, and migrations.