本文へ移動
cccskills
無料GitHub で公開日本語紹介

java-hibernate-jpa

Javaのデータベース操作で起きるクエリの増加や遅延読み込みエラーを調べ、エンティティ設計や関連付けを見直します。Hibernateの版による違いも扱います。

原文原文の説明を見る

Diagnose and fix Hibernate ORM / Jakarta Persistence problems in plain Java: N+1 queries, lazy loading errors, identifier generation, entity equals/hashCode, cascades, pagination, batching, locking, caching, Lombok on entities, and Hibernate 6/7 differences. Use when reviewing or writing JPA entities, mappings, and queries. Framework-neutral.

実行スコア 未検証

インストール方法を見る

こんなときに便利

  • 大量に発行されるSQLの原因調査
  • 遅延読み込みエラーを直したいとき
  • 関連付けや削除設定のレビュー
  • ページ分割や一括登録の改善

日本語での紹介

できること

Hibernate ORMとJakarta Persistenceを使うJavaコードで、データベース操作の不具合や性能問題を調べます。関連データの取得で問い合わせが増えるN+1問題、必要になった時点でデータを読む遅延読み込みのエラー、ページ分割、一括処理を扱います。エンティティと呼ばれる保存対象のクラスについて、ID生成、関連付け、equalsとhashCode、連動する削除の設定も見直します。

こんなときに便利

画面表示のたびに似たSQLが大量に出る場合や、更新の競合、意図しない関連データの削除を調べたい場合に向いています。SQLの実行回数を測り、実際のデータベースで改善を確かめる方針です。Hibernateのバージョンによる挙動の違いも確認します。

使い方の例

  • 「一覧取得でN+1が起きていないか、SQLの回数から確認して」
  • 「LazyInitializationExceptionの原因と取得方法を見直して」
  • 「エンティティの関連付けと削除設定をレビューして」

注意点

利用中のHibernate、Persistence API、データベースの確認が前提です。Hibernate 7にはJava 17以上が必要です。本番のスキーマ管理はFlywayやLiquibaseによる変更履歴の管理を基本とし、外部管理のスキーマには移行ツールを追加しない方針です。

この紹介文は、公開されている SKILL.md をもとに AI(OpenAI Codex)が作成しました。正確な仕様は下の原文を確認してください。

含まれるファイル(7)

  • SKILL.md6.5 KB
  • references/associations-and-cascade.md4.4 KB
  • references/fetching-and-n-plus-one.md5.8 KB
  • references/identifiers-and-entity-design.md7.1 KB
  • references/locking-transactions-caching.md5.7 KB
  • references/queries-pagination-batching.md6.8 KB
  • references/tooling-and-versions.md6.1 KB

SKILL.md(原文)

インストールする前に、エージェントに与えられる指示の中身を確認できます。

Hibernate ORM and Jakarta Persistence

Problem -> anti-pattern -> fix library for entity mapping and querying. Framework-neutral: it covers EntityManager/Session behavior, JPQL/HQL, and mapping annotations. If the project uses Spring Boot and the java-springboot and spring-boot-testing skills are installed, use them for Spring Data repositories, transaction wiring, and @DataJpaTest; for Panache and reactive persistence see the Quarkus skills. Integration tests against a real database: java-testcontainers.

First: detect the stack

  1. Read pom.xml/build.gradle (or the dependency tree) for hibernate-core and the persistence API: jakarta.persistence (Hibernate 6+/Jakarta EE 9+) vs javax.persistence (Hibernate 5, see javax-to-jakarta-migration).
  2. Note the Hibernate major version; behavior differs (see references/tooling-and-versions.md). Hibernate 6.0-6.1 implement Jakarta Persistence 3.0, 6.2+ implement 3.1, 7.x implements 3.2, and Hibernate 7 needs Java 17+.
  3. Note the database and whether the schema is managed by Flyway/Liquibase.
  4. Turn on SQL logging and statistics in a test (see references/tooling-and-versions.md) before "optimizing" anything: measure statement counts.

Quick decision table

SymptomLikely causeGo to
Many similar SELECTs for one requestN+1 on lazy associationreferences/fetching-and-n-plus-one.md
LazyInitializationExceptionLazy access outside sessionreferences/fetching-and-n-plus-one.md
HHH000104 / HHH90003004 warning, big memory usePagination with collection fetch joinreferences/queries-pagination-batching.md
Inserts slow, no JDBC batchingIDENTITY ids or batching not configuredreferences/identifiers-and-entity-design.md, references/queries-pagination-batching.md
Duplicate rows / lost children / Set anomaliesBad equals/hashCode or cascade misusereferences/identifiers-and-entity-design.md, references/associations-and-cascade.md
Lost updates, deadlocksMissing locking strategyreferences/locking-transactions-caching.md
Stale or surprising data with cachesSecond-level/query cache misusereferences/locking-transactions-caching.md
Schema drift, ddl-auto in prodNo migration toolreferences/tooling-and-versions.md

Top rules (apply during review)

  1. Associations: @ManyToOne/@OneToOne default to EAGER; declare fetch = FetchType.LAZY explicitly. @OneToMany/@ManyToMany are LAZY by default and should stay so. Choose fetching per query (JOIN FETCH, entity graph), not by making mappings EAGER.
  2. Prefer @ManyToOne owning side plus a bidirectional @OneToMany(mappedBy=...) only when needed; keep both sides in sync through helper methods.
  3. Set or List for collections? Use Set for @ManyToMany; a unidirectional @OneToMany with @JoinColumn or List without an order has costly update patterns.
  4. Never use CascadeType.REMOVE/ALL on @ManyToOne or @ManyToMany. Use orphanRemoval only on a true parent-owned child collection.
  5. Ids: SEQUENCE (pooled optimizer, allocationSize matching the DB increment) for generated numeric ids; IDENTITY disables JDBC insert batching; use UUIDs (time-ordered preferred) when ids must be generated client-side.
  6. Entities: no Lombok @Data/@EqualsAndHashCode/@ToString over associations; implement equals/hashCode deliberately.
  7. Return DTOs/projections from read endpoints rather than entities; never expose entities (lazy proxies) directly in an API.
  8. Paginate with a stable ORDER BY including a unique tiebreaker; never combine setMaxResults with a collection JOIN FETCH.
  9. Keep transactions short, at the service boundary; no remote calls or user interaction inside one.
  10. Write and flush in batches for bulk work and clear the persistence context periodically.
  11. Verify behavior with statement counts in tests against the real database engine.
  12. Follow the schema management rule below.

Schema management rule

This is the single rule other skills refer to by name:

  • Prototype/local: hibernate.hbm2ddl.auto=update (or create-drop for throwaway tests) is acceptable.
  • Production (default): versioned migrations with Flyway or Liquibase, and hibernate.hbm2ddl.auto=validate (or none). Add them when the user wants versioned migrations or the app is headed to production.
  • Externally managed schema: none (or validate); do not add a migration tool.

Review focus

Check statement counts for N+1, explicit LAZY on to-one associations, sequence-based ids with matching allocationSize, and equals/hashCode stable across transient to persistent. Version-dependent details (Hibernate 5/6/7): references/tooling-and-versions.md.

References

  • references/fetching-and-n-plus-one.md: N+1 detection and fixes, lazy vs eager, LazyInitializationException, open-session-in-view, DTO projections.
  • references/identifiers-and-entity-design.md: id generation, equals/hashCode, Lombok, records vs entities, enums and converters, value types.
  • references/associations-and-cascade.md: bidirectional helpers, cascade, orphanRemoval, collection choice, @ManyToMany.
  • references/queries-pagination-batching.md: pagination (offset, keyset, in-memory warning), bulk operations, JDBC batching, StatelessSession.
  • references/locking-transactions-caching.md: optimistic/pessimistic locking, transaction boundaries, flush modes, second-level cache.
  • references/tooling-and-versions.md: SQL logging, statistics, tests, connection pool sizing, migrations, Hibernate 5/6/7 differences.

Canonical docs: Hibernate ORM user guide https://docs.hibernate.org/orm/current/userguide/html_single/ and Jakarta Persistence https://jakarta.ee/specifications/persistence/. Deep background articles: https://vladmihalcea.com/.

Upgrading (fetch the page for the target version; prefer it over this skill): migration guides https://hibernate.org/orm/documentation/migrate/ (6.6: https://docs.hibernate.org/orm/6.6/migration-guide/, 7.0: https://docs.hibernate.org/orm/7.0/migration-guide/), release notes https://hibernate.org/orm/releases/.

レビュー

まだレビューはありません。使ってみた感想をお寄せください。

コメント

ログインなしで書き込めます。使ってみた感想、質問、動かなかった環境などを気軽にどうぞ。

まだコメントはありません。最初のコメントを書いてみませんか。

送信するとすぐに公開されます。メールアドレスや電話番号などの個人情報は書かないでください。URL は 2 つまでです。

0 / 1000 文字

このフォームは reCAPTCHA で保護されており、Google のプライバシーポリシーと利用規約が適用されます。

同じリポジトリのスキル

acquire-codebase-knowledge

無料概要あり

既存プロジェクトのコードや設定を調べ、技術構成・設計・規約・テスト・懸念点などを7つの文書に整理します。根拠のファイルと不明点も明記し、引き継ぎに役立てます。

  • 初めて触るコードの全体像を知りたいとき
  • プロジェクトの引き継ぎ資料作成
  • 設計資料と実装の違いを確認
github/awesome-copilot4万2026年10月24日 更新

acreadiness-assess

無料概要あり

リポジトリのAI活用準備度をAgentRCで評価し、9つの観点のスコアや改善案をHTMLにまとめます。組織独自の採点方針にも対応します。

  • AIエージェント導入前の準備度確認
  • 評価結果から改善の優先順位を決めたいとき
  • 組織独自の基準で採点したいとき
github/awesome-copilot4万2026年10月24日 更新

実際のコードから言語や開発ルールを読み取り、AIコーディングエージェント向けの指示ファイルを生成。プロジェクト全体や領域別にルールを整理します。

  • VS Code Copilot向け指示を作成
  • 開発ルールの変更を指示に反映
  • モノレポの領域別ルールを整理
github/awesome-copilot4万2026年10月24日 更新

acreadiness-policy

無料概要あり

AgentRCの準備度評価を、組織やチームの基準に合わせて調整します。チェックの重みや合格率、除外項目を設定し、CIでの合否判定にも使えます。

  • 組織共通の評価基準づくり
  • AI関連の整備状況だけを評価
  • チェックの重要度を調整したいとき
github/awesome-copilot4万2026年10月24日 更新

ad-campaign-analyzer

無料概要あり

広告の実績データから無駄な支出と成果の高い施策を見つけ、停止・増額・検証の候補を整理します。媒体をまたぐ予算配分の見直しやA/Bテストの評価にも使えます。

  • 停止すべき広告やキーワードの特定
  • 増額候補のキャンペーンや配信対象の発見
  • A/Bテスト結果の有意性を確認したいとき
github/awesome-copilot4万2026年10月24日 更新

add-educational-comments

無料概要あり

指定したコードファイルに、構文や設計の理由を説明する学習用コメントを追加します。読む人の知識に合わせて解説の深さや繰り返し、注釈番号の有無を調整できます。

  • 既存コードに初心者向け解説を付けたいとき
  • 構文や設計を選んだ理由の説明
  • 読み手の経験に合わせたコメント調整
github/awesome-copilot4万2026年10月24日 更新

github のスキルをすべて見る

このスキルの問題を報告する