ワンクリックで
jpa-patterns
Spring Boot 中用于实体设计、关联关系、查询优化、事务、审计、索引、分页和连接池的 JPA/Hibernate 模式。
Codex または Claude でインストール この Prompt をコピーして Codex、Claude、または他のアシスタントに貼り付けると、Skill ページを確認してインストールできます。
メニュー
Spring Boot 中用于实体设计、关联关系、查询优化、事务、审计、索引、分页和连接池的 JPA/Hibernate 模式。
Codex または Claude でインストール この Prompt をコピーして Codex、Claude、または他のアシスタントに貼り付けると、Skill ページを確認してインストールできます。
SOC 職業分類に基づく
发现技能 (Skill),迭代查询,并在任何环境中自动安装技能。
PostgreSQL database patterns for query optimization, schema design, indexing, and security. Based on Supabase best practices.
后端架构模式、API 设计、数据库优化,以及 Node.js、Express 和 Next.js API 路由的服务端最佳实践。
ClickHouse 数据库模式、查询优化、分析以及针对高性能分析工作负载的数据工程最佳实践。
适用于 TypeScript、JavaScript、React 和 Node.js 开发的通用编码标准、最佳实践和模式。
自动从 Claude Code 会话(Sessions)中提取可重用的模式,并将其保存为学习到的技能以供未来使用。
| name | jpa-patterns |
| description | Spring Boot 中用于实体设计、关联关系、查询优化、事务、审计、索引、分页和连接池的 JPA/Hibernate 模式。 |
用于 Spring Boot 中的数据建模、存储层(Repositories)开发和性能调优。
@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 {}
@OneToMany(mappedBy = "market", cascade = CascadeType.ALL, orphanRemoval = true)
private List<PositionEntity> positions = new ArrayList<>();
JOIN FETCHEAGER);对于读取路径,优先使用 DTO 投影(Projections)@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 注解 Service 方法@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);
对于游标式分页(Cursor-like pagination),请在 JPQL 中包含 id > :lastId 并配合排序。
status、slug、外键)添加索引(Indexing)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 配合 Testcontainers 来模拟生产环境logging.level.org.hibernate.SQL=DEBUG 以及 logging.level.org.hibernate.orm.jdbc.bind=TRACE 以查看参数值记住:保持实体精简、查询意图明确且事务简短。通过抓取策略(Fetch strategies)和投影(Projections)来防止 N+1 问题,并针对读/写路径建立索引。