何时激活
设计 JPA 实体和表映射时 定义关系时 (@OneToMany, @ManyToOne, @ManyToMany) 优化查询时 (N+1 问题预防、获取策略、投影) 配置事务、审计或软删除时 设置分页、排序或自定义存储库方法时 调整连接池 (HikariCP) 或二级缓存时
affaan-m/ECC
Review jpa-patterns's use cases, installation, workflow, and original source instructions.
npx skills add https://github.com/affaan-m/ECC --skill "docs/zh-CN/skills/jpa-patterns"Source checked Jul 28, 2026·Refresh due Oct 26, 2026
Reorganized from the pinned upstream SKILL.md
用于 Spring Boot 中的数据建模、存储库和性能调优。
npx skills add https://github.com/affaan-m/ECC --skill "docs/zh-CN/skills/jpa-patterns"The pinned source supports a structured brief, but not an expanded tutorial. Only detected inputs, outputs, and sections are shown.
283 source words · 11 usable sections
Documentation workflow
Sections are extracted automatically from the pinned SKILL.md and link back to the source.
设计 JPA 实体和表映射时 定义关系时 (@OneToMany, @ManyToOne, @ManyToMany) 优化查询时 (N+1 问题预防、获取策略、投影) 配置事务、审计或软删除时 设置分页、排序或自定义存储库方法时 调整连接池 (HikariCP) 或二级缓存时
Review the “实体设计” section in the pinned source before continuing.
默认使用延迟加载;需要时在查询中使用 JOIN FETCH 避免在集合上使用 EAGER;对于读取路径使用 DTO 投影
使用投影进行轻量级查询:
Documentation checklist
The source section “何时激活” has been checked.
The source section “实体设计” has been checked.
The source section “关联关系和 N+1 预防” has been checked.
The source section “存储库模式” has been checked.
Choose a different workflow
JPA/Hibernate patterns for entity design, relationships, query optimization, transactions, auditing, indexing, pagination, and pooling in Spring Boot.
A separate implementation from affaan-m/ECC; compare its source, maintenance signals, and permission requirements.
Open source detailJPA/Hibernate patterns for entity design, relationships, query optimization, transactions, auditing, indexing, pagination, and pooling in Spring Boot.
A separate implementation from affaan-m/ECC; compare its source, maintenance signals, and permission requirements.
Open source detailPatrones JPA/Hibernate para diseño de entidades, relaciones, optimización de consultas, transacciones, auditoría, indexación, paginación y pooling en Spring Boot.
A separate implementation from affaan-m/ECC; compare its source, maintenance signals, and permission requirements.
Open source detailFAQ
用于 Spring Boot 中的数据建模、存储库和性能调优。
The source record exposes this install command: npx skills add https://github.com/affaan-m/ECC --skill "docs/zh-CN/skills/jpa-patterns". Inspect the command and pinned source before running it.
Quality breakdown
Based on traceable docs and repository signals; stars are not treated as quality.
Compare before choosing
These links are selected from shared tasks, functions, stacks, platforms, and same-name variants. Compare the source owner, documentation, permissions, and maintenance signals.
JPA/Hibernate patterns for entity design, relationships, query optimization, transactions, auditing, indexing, pagination, and pooling in Spring Boot.
JPA/Hibernate patterns for entity design, relationships, query optimization, transactions, auditing, indexing, pagination, and pooling in Spring Boot.
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.
Spring Boot'ta entity tasarımı, ilişkiler, sorgu optimizasyonu, transaction'lar, auditing, indeksleme, sayfalama ve pooling için JPA/Hibernate kalıpları.
JPA/Hibernate patterns for entity design, relationships, query optimization, transactions, auditing, indexing, pagination, and pooling in Spring Boot.
用于 Spring Boot 中的数据建模、存储库和性能调优。
@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 {}
@OneToMany(mappedBy = "market", cascade = CascadeType.ALL, orphanRemoval = true)
private List<PositionEntity> positions = new ArrayList<>();
JOIN FETCHEAGER;对于读取路径使用 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 进行批量写入推荐属性:
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
@DataJpaTest 来镜像生产环境logging.level.org.hibernate.SQL=DEBUG 和 logging.level.org.hibernate.orm.jdbc.bind=TRACE 以查看参数值请记住:保持实体精简,查询有针对性,事务简短。通过获取策略和投影来预防 N+1 问题,并根据读写路径建立索引。