affaan-m/ECC

jpa-patterns

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

64Collecting
See how to use itView GitHub source
npx skills add https://github.com/affaan-m/ECC --skill "docs/ja-JP/skills/jpa-patterns"
Automated source guideDocumentationStandard source

Source checked Jul 28, 2026·Refresh due Oct 26, 2026

Reorganized from the pinned upstream SKILL.md

Source-grounded documentation guide: jpa-patterns

Spring Bootでのデータモデリング、リポジトリ、パフォーマンスチューニングに使用します。

npx skills add https://github.com/affaan-m/ECC --skill "docs/ja-JP/skills/jpa-patterns"
Check the pinned source

The pinned source supports a structured brief, but not an expanded tutorial. Only detected inputs, outputs, and sections are shown.

278 source words · 10 usable sections

Documentation workflow

Read jpa-patterns through these 4 source sections

Sections are extracted automatically from the pinned SKILL.md and link back to the source.

01

エンティティ設計

Review the “エンティティ設計” section in the pinned source before continuing.

SKILL.md · エンティティ設計
Review and apply the “エンティティ設計” source section.
02

リレーションシップとN+1防止

デフォルトで遅延ロード。必要に応じてクエリで JOIN FETCH を使用

SKILL.md · リレーションシップとN+1防止
デフォルトで遅延ロード。必要に応じてクエリで JOIN FETCH を使用コレクションでは EAGER を避け、読み取りパスにはDTOプロジェクションを使用- デフォルトで遅延ロード。必要に応じてクエリで JOIN FETCH を使用 - コレクションでは EAGER を避け、読み取りパスにはDTOプロジェクションを使用
04

トランザクション

サービスメソッドに @Transactional を付ける

SKILL.md · トランザクション
サービスメソッドに @Transactional を付ける読み取りパスを最適化するために @Transactional(readOnly = true) を使用伝播を慎重に選択。長時間実行されるトランザクションを避ける

Documentation checklist

Verify each item before delivery

The source section “エンティティ設計” has been checked.

The source section “リレーションシップとN+1防止” has been checked.

The source section “リポジトリパターン” has been checked.

The source section “トランザクション” has been checked.

Choose a different workflow

When another Skill is the better fit

FAQ

What does the jpa-patterns source document cover?

Spring Bootでのデータモデリング、リポジトリ、パフォーマンスチューニングに使用します。

How do I install jpa-patterns?

The source record exposes this install command: npx skills add https://github.com/affaan-m/ECC --skill "docs/ja-JP/skills/jpa-patterns". Inspect the command and pinned source before running it.

Repository stars
234,327
Repository forks
35,711
Quality
64/100
Source repository last pushed

Quality breakdown

Based on traceable docs and repository signals; stars are not treated as quality.

64/100
Documentation21/30
Specificity13/25
Maintenance18/20
Trust signals12/25
View original Skill.mdThis page is parsed directly from the repository SKILL.md without editorial rewriting. Collected: Jul 28, 2026 · about 1 min

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を防ぎ、読み取り/書き込みパスにインデックスを作成します。

Source repo
affaan-m/ECC
Skill path
docs/ja-JP/skills/jpa-patterns/SKILL.md
Commit SHA
4e973d3eaf92
Repository license
MIT
Data collected