AgentStack
Browse Sign in
Browse Why AgentStack Sell Docs
Sign in
SKILL verified MIT Self-run

Jpa Patterns

skill-xu-xiang-everything-claude-code-zh-jpa-patterns · by xu-xiang

Spring Boot 中用于实体设计、关联关系、查询优化、事务、审计、索引、分页和连接池的 JPA/Hibernate 模式。

No reviews yet
0 installs
6 views
0.0% view→install

Install

$ agentstack add skill-xu-xiang-everything-claude-code-zh-jpa-patterns

✓ scanned · ✓ verified, works with Claude Code, Cursor, and more.

Security review

✓ Passed

No issues found. Passed automated security review. · v0.1.0 How review works →

  • Prompt-injection patterns
  • Secret / credential exfiltration
  • Dangerous shell & filesystem operations
  • Untrusted network calls
  • Known-malicious package signatures

What it can access

  • Network access No
  • Filesystem access No
  • Shell / process execution No
  • Environment & secrets No
  • Dynamic code execution No

From automated source analysis of v0.1.0. “Used” means the capability is present in the source — more access means more to trust, not that it’s unsafe.

View the full security report →

Verified badge

Passed review? Show it. Paste this badge into your README, it links to the public security report.

AgentStack Verified badge Links to your public security report.
[![AgentStack Verified](https://agentstack.voostack.com/badges/verified.svg)](https://agentstack.voostack.com/security/report/skill-xu-xiang-everything-claude-code-zh-jpa-patterns)

Reliability & compatibility

Security review passed
0 installs to date
no reviews yet
5mo ago

Declared compatibility

Claude CodeClaude Desktop

Compatibility is declared by the source manifest. End-to-end runtime verification is coming, see below.

Preview Execution monitoring

We're building live execution health for every listing: tool-call success rate, median latency, uptime, and last-checked timestamps, measured, not self-reported. It isn't live yet, so we don't show numbers we can't stand behind.

How agent discovery & health will work →
Are you the author of Jpa Patterns? Claim this listing to set pricing, connect Stripe payouts, and keep 70% of every sale.
Sign up to claim

About

JPA/Hibernate 模式

用于 Spring Boot 中的数据建模、存储库(Repository)和性能调优。

实体设计 (Entity Design)

@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 问题预防

@OneToMany(mappedBy = "market", cascade = CascadeType.ALL, orphanRemoval = true)
private List positions = new ArrayList<>();
  • 默认使用延迟加载(Lazy Loading)。根据需要,在查询中使用 JOIN FETCH
  • 集合属性应避免使用 EAGER 加载,在读取路径中优先使用 DTO 投影(Projection)。
@Query("select m from MarketEntity m left join fetch m.positions where m.id = :id")
Optional findWithPositions(@Param("id") Long id);

存储库模式 (Repository Pattern)

public interface MarketRepository extends JpaRepository {
  Optional findBySlug(String slug);

  @Query("select m from MarketEntity m where m.status = :status")
  Page findByStatus(@Param("status") MarketStatus status, Pageable pageable);
}
  • 对于轻量级查询,请使用投影(Projection):
public interface MarketSummary {
  Long getId();
  String getName();
  MarketStatus getStatus();
}
Page findAllBy(Pageable pageable);

事务 (Transactions)

  • 在服务层(Service)方法上添加 @Transactional 注解。
  • 使用 @Transactional(readOnly = true) 优化只读路径。
  • 谨慎选择事务传播(Propagation)行为。避免长时间运行的事务。
@Transactional
public Market updateStatus(Long id, MarketStatus status) {
  MarketEntity entity = repo.findById(id)
      .orElseThrow(() -> new EntityNotFoundException("Market"));
  entity.setStatus(status);
  return Market.from(entity);
}

分页 (Pagination)

PageRequest page = PageRequest.of(pageNumber, pageSize, Sort.by("createdAt").descending());
Page markets = repo.findByStatus(MarketStatus.ACTIVE, page);

对于类似游标(Cursor)的分页,请在 JPQL 排序中包含 id > :lastId

索引创建与性能优化

  • 为常用过滤器(如 statusslug、外键)添加索引。
  • 使用符合查询模式的复合索引(例如 status, created_at)。
  • 避免使用 select *,仅投影必要的列。
  • 利用 saveAllhibernate.jdbc.batch_size 进行批量写入。

连接池 (Connection Pooling - 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

缓存 (Caching)

  • 一级缓存(First-level Cache)是基于 EntityManager 的。不要在事务之间持有实体。
  • 对于读取密集型实体,请谨慎考虑二级缓存(Second-level Cache)。验证淘汰策略(Eviction Strategy)。

数据库迁移 (Migration)

  • 使用 Flyway 或 Liquibase。不要在生产环境中依赖 Hibernate 的自动 DDL。
  • 保持迁移脚本的幂等性(Idempotent)和增量性。不要在未规划的情况下删除列。

数据访问测试

  • 优先使用配合 Testcontainers 的 @DataJpaTest,以真实反映生产环境。
  • 通过日志断言 SQL 效率:将 logging.level.org.hibernate.SQL 设置为 DEBUG,将 logging.level.org.hibernate.orm.jdbc.bind 设置为 TRACE 以查看参数值。

注意:保持实体轻量化,确保查询意图明确,并缩短事务时长。通过抓取策略(Fetch Strategy)和投影(Projection)防止 N+1 问题,并为读/写路径创建索引。

Source & license

This open-source skill is cataloged on AgentStack and links to its original source — we do not rehost the code.

Install and usage instructions live in the source repository linked above.

Reviews

No reviews yet, be the first.

Versions

  • v0.1.0 Imported from the upstream source.