jpa-patterns
Master JPA entity modeling, repository implementation, and performance tuning for Spring Boot applications. Learn to configure relationships, prevent N+1 query issues through fetch strategies and projections, set up transactional boundaries, and optimize database connections with HikariCP.
jpa-patterns helps you design JPA entities with relationships and optimize queries to prevent N+1 problems in Spring Boot.
AI-generated summary based on this skill's SKILL.md
Install
xu-xiang/everything-claude-code-zh/jpa-patterns · repository language: JavaScript
git clone https://github.com/xu-xiang/everything-claude-code-zh
cp -r everything-claude-code-zh/skills/jpa-patterns ~/.claude/skills/jpa-patternsnpx skillfed install xu-xiang/everything-claude-code-zh/jpa-patternsFrequently asked questions
AI-generated answers based on this skill's SKILL.md and metadata
How does jpa-patterns help prevent N+1 query problems?
jpa-patterns teaches you to identify and eliminate N+1 query issues through strategic fetch strategies and projections. The skill covers lazy vs. eager loading trade-offs, using @Query with JOIN FETCH to load related entities in a single query, and implementing DTO projections to fetch only needed columns. You'll learn when to apply each technique based on your access patterns.
What entity design and JPA relationships does jpa-patterns cover?
jpa-patterns covers OneToMany, ManyToOne, ManyToMany, and OneToOne relationships with practical entity design patterns. You'll learn how to model bidirectional vs. unidirectional relationships, configure cascade types appropriately, and use @JoinColumn and @JoinTable annotations correctly. The skill emphasizes designing relationships that support efficient queries and prevent common pitfalls.
How do I configure transaction management and auditing in jpa-patterns?
jpa-patterns teaches Spring Boot transaction management using @Transactional with proper propagation and isolation levels. For auditing, you'll learn to use @CreatedDate, @LastModifiedDate, and @EntityListeners to automatically track entity changes. The skill covers setting up AuditorAware beans to capture user information and configuring audit tables for compliance.
What Hibernate performance tuning techniques are included?
jpa-patterns covers Hibernate performance optimization including connection pooling with HikariCP, composite index design for query acceleration, and second-level caching strategies. You'll learn batch insert operations with saveAll, lazy loading configuration, and monitoring query execution. The skill helps you identify bottlenecks and apply targeted tuning.
Can jpa-patterns teach custom repository methods and projections?
Yes. jpa-patterns covers implementing custom repository methods using Spring Data's @Query annotation with both JPQL and native SQL. You'll learn DTO projections to fetch only required fields, reducing memory overhead and network traffic. The skill includes examples of interface-based and class-based projections for flexible data access patterns.
Does jpa-patterns include database migrations and testing best practices?
jpa-patterns includes database migration setup using Flyway with Spring Boot, covering versioning strategies and rollback patterns. For testing, you'll learn to set up test data access layers, use embedded databases like H2, and write integration tests for repositories. The skill emphasizes reproducible test environments and data consistency.
SKILL.md
rendered from the published skill — quoted content, verbatim
JPA/Hibernate 模式
用于 Spring Boot 中的数据建模、仓库层(Repositories)实现和性能调优。
何时激活
- 设计 JPA 实体(Entities)和表映射
- 定义关联关系(@OneToMany、@ManyToOne、@ManyToMany)
- 优化查询(防止 N+1 问题、抓取策略、投影)
- 配置事务、审计(Auditing)或逻辑删除
- 设置分页、排序或自定义 Repository 方法
- 调优连接池(HikariCP)或二级缓存(Second-level Caching)
实体设计
@Entity
@Table(name = "markets", indexes = {
@Index(name = "idx_markets_slug", columnList = "slug", unique = true)
})
@EntityListeners(AuditingEntityListener.class)
public class MarketEntity {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, length = 200)
private String name;
@Column(nullable = false, unique = true, length = 120)
private String slug;
@Enumerated(EnumType.STRING)
private MarketStatus status = MarketStatus.ACTIVE;
@CreatedDate private Instant createdAt;
@LastModifiedDate private Instant updatedAt;
}
启用审计(Auditing):
@Configuration
@EnableJpaAuditing
class JpaConfig {}
关联关系与防止 N+1 问题
```java @OneToMany(mappedBy = "market", cascade = CascadeType.ALL, orphanRemoval =
(truncated - see the full file via the links below)
Read as markdown · JSON record · Browse the source repository
File tree — 1 file
skills/jpa-patterns/SKILL.md