java-jpa-hibernate

Design JPA entities, optimize Hibernate queries, and configure transactions and caching.

Updated Dec 26, 2025
One-click install
npx skills add https://github.com/jwvdvuurst/CalculatorCollection --skill java-jpa-hibernate-jwvdvuurst
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: java-jpa-hibernate
Source: https://github.com/jwvdvuurst/CalculatorCollection/tree/main/skills/java-jpa-hibernate
Command: npx skills add https://github.com/jwvdvuurst/CalculatorCollection --skill java-jpa-hibernate-jwvdvuurst

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires pyyaml, and includes scripts (resource) and references (resource) and assets (resource) components.

What problem does it solve? Building persistence layers with JPA and Hibernate often leads to N+1 query problems, LazyInitializationException errors, and poorly tuned connection pools that degrade production performance. This Skill provides concrete patterns for entity design, query optimization, transaction management, and caching so you can build efficient data access layers without trial and error. ## Core Features & Use Cases - Entity Design: Map relationships (1:1, 1:N, N:M), inheritance strategies, auditing with @CreatedDate/@LastModifiedDate, and bidirectional association helpers. - Query Optimization: Prevent N+1 queries using JOIN FETCH, EntityGraph, @BatchSize, and DTO projections, with a decision table for choosing the right strategy. - Transactions & Caching: Configure @Transactional propagation and isolation, optimistic locking with @Version, and first/second-level caches with Redis integration. - Use Case: When a Spring Boot endpoint triggers hundreds of SQL statements per request, apply the JOIN FETCH and batch fetching patterns here to reduce it to a single optimized query. ## Quick Start Ask the assistant to review your JPA entities and repositories for N+1 query problems and recommend the appropriate fetch strategy.

Frequently Asked Questions about java-jpa-hibernate

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

FAQPage Schema
How do I prevent N+1 queries in JPA and Hibernate?

Prevent N+1 queries by using JOIN FETCH in JPQL for relations you always need, @EntityGraph for dynamic fetching, or @BatchSize for collection access. For read-only queries, DTO projections avoid loading entities entirely.

What is the difference between JOIN FETCH and EntityGraph in JPA?

JOIN FETCH is written directly in the JPQL query and always fetches the relation, while @EntityGraph declares fetch paths on the repository method for more dynamic control. Both solve N+1 problems; choose based on whether the fetch plan varies per use case.

Why does LazyInitializationException happen in Spring Boot?

LazyInitializationException occurs when a lazy-loaded association is accessed after the Hibernate session closes, typically because spring.jpa.open-in-view is disabled. Fix it by fetching data within the transaction using JOIN FETCH or DTO projections.

How do I configure Hibernate batch inserts in Spring Boot?

Set hibernate.jdbc.batch_size (e.g., 50), hibernate.order_inserts=true, and hibernate.order_updates=true in your application properties. Batching groups SQL statements into fewer round trips, significantly improving bulk write performance.

When should I use optimistic vs pessimistic locking in JPA?

Use optimistic locking with @Version for most concurrent update scenarios since it avoids database locks and scales better. Reserve pessimistic locking for high-contention rows where conflicts are frequent and retries are costly.