mcpbeat Sign in

Jpa Patterns Skill for Claude

JPA/Hibernate patterns for entity design, relationships, query optimization, transactions, auditing, indexing, pagination, and pooling in Spring Boot.

1k tokens
context cost
the whole folder, loaded on every use
1
files
instructions only
0
copies elsewhere
how many repositories repackaged it
265
stars on the repo
on the repository, not the skill itself

Install

one command, takes just this skill from the repository
npx skills add https://github.com/loulanyue/awesome-claude-notes --skill jpa-patterns

The instruction itself

3 sections, as written by the author

JPA/Hibernate パターン

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 {}

リレーションシップと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 を含める。

インデックス作成とパフォーマンス

  • 一般的なフィルタ(statusslug、外部キー)にインデックスを追加
  • クエリパターンに一致する複合インデックスを使用(status, created_at
  • select * を避け、必要な列のみを投影
  • saveAllhibernate.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

キャッシング

  • 1次キャッシュはEntityManagerごと。トランザクション間でエンティティを保持しない
  • 読み取り集約型エンティティには、2次キャッシュを慎重に検討。退避戦略を検証

マイグレーション

  • FlywayまたはLiquibaseを使用。本番環境でHibernate自動DDLに依存しない
  • マイグレーションを冪等かつ追加的に保つ。計画なしに列を削除しない

データアクセステスト

  • 本番環境を反映するために、Testcontainersを使用した @DataJpaTest を優先
  • ログを使用してSQL効率をアサート: パラメータ値には logging.level.org.hibernate.SQL=DEBUGlogging.level.org.hibernate.orm.jdbc.bind=TRACE を設定

注意: エンティティを軽量に保ち、クエリを意図的にし、トランザクションを短く保ちます。フェッチ戦略とプロジェクションでN+1を防ぎ、読み取り/書き込みパスにインデックスを作成します。

原文

  • 英語版の原文

ナビゲーション

  • 日本語ドキュメント一覧
  • skills/README.md
  • 貢献ガイド

Other skills for the same job

different authors, same section of the catalogue
Protocolsio Integration
by christophacham
×4

Integration with protocols.io API for managing scientific protocols. This skill should be used when working with protocols.io to search, create, update, or publish protocols; manage protocol steps and materials; handle discussions and comments; organize workspaces; upload and manage files; or integrate protocols.io functionality into workflows. Applicable for protocol discovery, collaborative protocol development, experiment tracking, lab protocol management, and scientific documentation.

16k tokens
Tailored Resume Generator
by frostant
×4

Analyzes job descriptions and generates tailored resumes that highlight relevant experience, skills, and achievements to maximize interview chances

3k tokens
Excalidraw Diagram Generator
by github
vendor ×3

Generate Excalidraw diagrams from natural language descriptions. Use when asked to "create a diagram", "make a flowchart", "visualize a process", "draw a system architecture", "create a mind map", or "generate an Excalidraw file". Supports flowcharts, relationship diagrams, mind maps, and system architecture diagrams. Outputs .excalidraw JSON files that can be opened directly in Excalidraw.

36k tokens scripts
Expo Dev Client
by openai
vendor ×3

Build and distribute Expo development clients locally or via TestFlight

961 tokens
Executing Plans
by ZhanlinCui
×3

Use when you have a written implementation plan to execute in a separate session with review checkpoints

542 tokens
Anndata
by christophacham
×3

Data structure for annotated matrices in single-cell analysis. Use when working with .h5ad files or integrating with the scverse ecosystem. This is the data format skill—for analysis workflows use scanpy; for probabilistic models use scvi-tools; for population-scale queries use cellxgene-census.

16k tokens
Benchling Integration
by christophacham
×3

Benchling R&D platform integration. Access registry (DNA, proteins), inventory, ELN entries, workflows via API, build Benchling Apps, query Data Warehouse, for lab data management automation.

14k tokens
Biopython
by christophacham
×3

Comprehensive molecular biology toolkit. Use for sequence manipulation, file parsing (FASTA/GenBank/PDB), phylogenetics, and programmatic NCBI/PubMed access (Bio.Entrez). Best for batch processing, custom bioinformatics pipelines, BLAST automation. For quick lookups use gget; for multi-service integration use bioservices.

24k tokens

How to use it

Copy the folder

Take loulanyue/jpa-patterns from the repository into ~/.claude/skills for personal use, or into .claude/skills inside a project.

Check the name does not clash

The agent identifies a skill by the name field in its header. Two skills with the same name cannot sit side by side — one of them will be ignored.