affaan-m/ECC/docs/zh-CN/skills/jpa-patterns/SKILL.md
jpa-patterns
Review jpa-patterns's use cases, installation, workflow, and original source instructions.
- Source repository stars
- 234,327
- Declared platforms
- 0
- Static risk flags
- 0
- Last source update
- 2026-07-27
- Source checked
- 2026-07-28
Decision brief
What it does—and where it fits
用于 Spring Boot 中的数据建模、存储库和性能调优。
Not for
- Tasks that require unconfirmed production actions or broad system permissions.
- Environments where the pinned source and install steps cannot be inspected.
Compatibility matrix
Platform support, with evidence labels
| Platform | Status | Evidence | What to check |
|---|---|---|---|
| Codex | Not declared | No explicit evidence | Portability before use |
| Claude Code | Not declared | No explicit evidence | Portability before use |
| Cursor | Not declared | No explicit evidence | Portability before use |
| Gemini CLI | Not declared | No explicit evidence | Portability before use |
Installation
Inspect first. Install second.
The source command is displayed only when detected. A safe inspection prompt is always available so your agent can explain every action before execution.
npx skills add https://github.com/affaan-m/ECC --skill "docs/zh-CN/skills/jpa-patterns"Inspect the Agent Skill "jpa-patterns" from https://github.com/affaan-m/ECC/blob/4e973d3eaf92d97f8d2e2d8abb39d8bdc8711b38/docs/zh-CN/skills/jpa-patterns/SKILL.md at commit 4e973d3eaf92d97f8d2e2d8abb39d8bdc8711b38. List every install step, command, network request, credential, file read/write, external action, and rollback step. Explain whether it fits my task. Do not install or execute anything until I approve.
Workflow
What the source asks the agent to do
- 01
何时激活
设计 JPA 实体和表映射时 定义关系时 (@OneToMany, @ManyToOne, @ManyToMany) 优化查询时 (N+1 问题预防、获取策略、投影) 配置事务、审计或软删除时 设置分页、排序或自定义存储库方法时 调整连接池 (HikariCP) 或二级缓存时
设计 JPA 实体和表映射时定义关系时 (@OneToMany, @ManyToOne, @ManyToMany)优化查询时 (N+1 问题预防、获取策略、投影) - 02
实体设计
Review the “实体设计” section in the pinned source before continuing.
Review and apply the “实体设计” source section. - 03
关联关系和 N+1 预防
默认使用延迟加载;需要时在查询中使用 JOIN FETCH 避免在集合上使用 EAGER;对于读取路径使用 DTO 投影
默认使用延迟加载;需要时在查询中使用 JOIN FETCH避免在集合上使用 EAGER;对于读取路径使用 DTO 投影默认使用延迟加载;需要时在查询中使用 JOIN FETCH 避免在集合上使用 EAGER;对于读取路径使用 DTO 投影 - 04
存储库模式
使用投影进行轻量级查询:
使用投影进行轻量级查询:
Permission review
Static risk signals and limitations
No configured static risk pattern was detected
This is not proof of safety. Runtime behavior, indirect dependencies, and hidden external systems are outside the static scan.
Evidence record
Why each signal appears
| Signal | Value | Evidence type | Meaning |
|---|---|---|---|
| Quality score | 63/100 | Computed | Documentation, specificity, maintenance, and trust rules |
| Repository stars | 234,327 | Source | Repository attention, not individual Skill quality |
| Compatibility | 0 platforms | Source | Declared in the catalog source record |
| Usage guide | automated source guide | Editorial | Generated or reviewed according to the visible evidence level |
Pinned source
Provenance and original SKILL.md
- Repository
- affaan-m/ECC
- Skill path
- docs/zh-CN/skills/jpa-patterns/SKILL.md
- Commit
- 4e973d3eaf92d97f8d2e2d8abb39d8bdc8711b38
- License
- MIT
- Collected
- 2026-07-28
- Default branch
- main
View the original SKILL.md
JPA/Hibernate 模式
用于 Spring Boot 中的数据建模、存储库和性能调优。
何时激活
- 设计 JPA 实体和表映射时
- 定义关系时 (@OneToMany, @ManyToOne, @ManyToMany)
- 优化查询时 (N+1 问题预防、获取策略、投影)
- 配置事务、审计或软删除时
- 设置分页、排序或自定义存储库方法时
- 调整连接池 (HikariCP) 或二级缓存时
实体设计
@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;
}
启用审计:
@Configuration
@EnableJpaAuditing
class JpaConfig {}
关联关系和 N+1 预防
@OneToMany(mappedBy = "market", cascade = CascadeType.ALL, orphanRemoval = true)
private List<PositionEntity> positions = new ArrayList<>();
- 默认使用延迟加载;需要时在查询中使用
JOIN FETCH - 避免在集合上使用
EAGER;对于读取路径使用 DTO 投影
@Query("select m from MarketEntity m left join fetch m.positions where m.id = :id")
Optional<MarketEntity> findWithPositions(@Param("id") Long id);
存储库模式
public interface MarketRepository extends JpaRepository<MarketEntity, Long> {
Optional<MarketEntity> findBySlug(String slug);
@Query("select m from MarketEntity m where m.status = :status")
Page<MarketEntity> findByStatus(@Param("status") MarketStatus status, Pageable pageable);
}
- 使用投影进行轻量级查询:
public interface MarketSummary {
Long getId();
String getName();
MarketStatus getStatus();
}
Page<MarketSummary> findAllBy(Pageable pageable);
事务
- 使用
@Transactional注解服务方法 - 对读取路径使用
@Transactional(readOnly = true)以进行优化 - 谨慎选择传播行为;避免长时间运行的事务
@Transactional
public Market updateStatus(Long id, MarketStatus status) {
MarketEntity entity = repo.findById(id)
.orElseThrow(() -> new EntityNotFoundException("Market"));
entity.setStatus(status);
return Market.from(entity);
}
分页
PageRequest page = PageRequest.of(pageNumber, pageSize, Sort.by("createdAt").descending());
Page<MarketEntity> markets = repo.findByStatus(MarketStatus.ACTIVE, page);
对于类似游标的分页,在 JPQL 中包含 id > :lastId 并配合排序。
索引和性能
- 为常用过滤器添加索引(
status、slug、外键) - 使用与查询模式匹配的复合索引(
status, created_at) - 避免
select *;仅投影需要的列 - 使用
saveAll和hibernate.jdbc.batch_size进行批量写入
连接池 (HikariCP)
推荐属性:
spring.datasource.hikari.maximum-pool-size=20
spring.datasource.hikari.minimum-idle=5
spring.datasource.hikari.connection-timeout=30000
spring.datasource.hikari.validation-timeout=5000
对于 PostgreSQL LOB 处理,添加:
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
缓存
- 一级缓存是每个 EntityManager 的;避免在事务之间保持实体
- 对于读取频繁的实体,谨慎考虑二级缓存;验证驱逐策略
迁移
- 使用 Flyway 或 Liquibase;切勿在生产中依赖 Hibernate 自动 DDL
- 保持迁移的幂等性和可添加性;避免无计划地删除列
测试数据访问
- 首选使用 Testcontainers 的
@DataJpaTest来镜像生产环境 - 使用日志断言 SQL 效率:设置
logging.level.org.hibernate.SQL=DEBUG和logging.level.org.hibernate.orm.jdbc.bind=TRACE以查看参数值
请记住:保持实体精简,查询有针对性,事务简短。通过获取策略和投影来预防 N+1 问题,并根据读写路径建立索引。
Alternatives
Compare before choosing
affaan-m/ECC
jpa-patterns
JPA/Hibernate patterns for entity design, relationships, query optimization, transactions, auditing, indexing, pagination, and pooling in Spring Boot.
affaan-m/ECC
jpa-patterns
JPA/Hibernate patterns for entity design, relationships, query optimization, transactions, auditing, indexing, pagination, and pooling in Spring Boot.
affaan-m/ECC
jpa-patterns
Patrones JPA/Hibernate para diseño de entidades, relaciones, optimización de consultas, transacciones, auditoría, indexación, paginación y pooling en Spring Boot.
affaan-m/ECC
jpa-patterns
Spring Boot'ta entity tasarımı, ilişkiler, sorgu optimizasyonu, transaction'lar, auditing, indeksleme, sayfalama ve pooling için JPA/Hibernate kalıpları.