jpa-spring-data-kotlin-mapper
JPA Spring Data Kotlin Mapper
Source mapping: Tier 1 critical skill derived from Kotlin_Spring_Developer_Pipeline.md (SK-10).
Mission
Generate and review persistence mappings that are correct for both Hibernate semantics and Kotlin semantics. Prevent bugs that compile cleanly but fail under lazy loading, dirty checking, identity comparison, or query load.
Read First
- Entity classes and mapped superclasses.
- Repository interfaces and custom queries.
- Relevant service methods and transaction boundaries.
- SQL logs, execution plans, or symptoms such as N+1, deadlocks, or stale reads.
- Build files to verify JPA-related Kotlin compiler plugins.
Entity Design Rules
- Do not generate JPA entities as
data class. - Keep transport DTOs and persistence entities separate unless the repository clearly uses a shared model on purpose.
- Model required columns as non-null only when object construction and persistence lifecycle make that safe.
- Treat lazy associations and optional associations carefully. Nullable is often the honest model.
- Use
lateinitonly when the project already accepts that tradeoff and the lifecycle is safe.
Identity And Equality Rules
- Never accept all-field
equalsandhashCodegenerated by adata classentity. - Follow project conventions when they already define an entity identity strategy.
- If no convention exists, choose an identity approach that is stable under persistence and proxying, then explain the tradeoff.
- Be explicit about mutable fields and lazy associations when discussing equality semantics.
Query And Fetch Rules
- Diagnose N+1 by looking at actual query count or SQL logs, not by guessing from annotations alone.
- Prefer targeted fetch solutions:
@EntityGraphJOIN FETCH- batch fetching
- DTO projection
- Be careful with collection fetch joins plus pagination. Call out the tradeoff instead of hiding it.
- Use indexes and uniqueness constraints to support real query patterns and idempotency guarantees.
Advanced ORM Traps
- Maintain both sides of bidirectional associations in domain methods or helper functions. Half-updated object graphs are a common source of subtle bugs.
orphanRemovaland cascade remove are not interchangeable. Explain the lifecycle semantics before choosing one.toString, debug logging, JSON serialization, and IDE inspection can trigger lazy loads. Treat them as potential side effects, not harmless utilities.- Bulk update or delete queries bypass the persistence context and lifecycle callbacks. Call out when subsequent reads may be stale until clear or reload.
- Multiple bag fetches can explode under Hibernate. If a collection-heavy fetch plan looks "obviously convenient," verify whether the ORM can execute it safely.
Set-based collections plus mutable equality are especially dangerous. Collection membership can break after entity state changes.@Versionis usually the clearest optimistic concurrency mechanism when concurrent updates matter. Mention it explicitly when the use case can lose updates.- If
open-in-viewis disabled, DTO mapping that touches lazy fields must happen inside a deliberate transaction boundary.
Expert Heuristics
- Choose entity shape for write correctness first and read shape second. If reads need a different shape, use projections or dedicated queries.
- If a query is hot and read-only, DTO projection is often a better optimization than tuning entity graphs indefinitely.
- If a relationship exists only to simplify one query, question whether it belongs in the entity model or in a query model.
- Treat database indexes as part of the persistence design, not as a late production optimization.
Kotlin-Specific Checks
- Verify
kotlin("plugin.jpa")or equivalent no-arg support when JPA entities exist. - Verify classes and members are compatible with proxying where needed.
- Verify nullability reflects database truth rather than wishful API design.
- Verify repository return types and service expectations agree on null handling.
Output Contract
Return these sections:
Persistence model: the entity and relationship shape that should exist.Correctness risks: identity, lazy loading, transactional access, and nullability traps.Performance risks: N+1, fetch plan, query shape, pagination, and index concerns.Recommended changes: entity, repository, and query updates in minimal-diff form.Verification: tests or SQL-level checks that confirm the mapping works as intended.
Guardrails
- Do not recommend
FetchType.EAGEReverywhere to silence lazy loading symptoms. - Do not expose entities directly through API responses by default.
- Do not use
data classentities. - Do not claim an N+1 fix without explaining how the fetch plan changes query behavior.
- Do not place all persistence intelligence in repositories if the service layer controls the real access pattern.
Quality Bar
A good run of this skill improves correctness and query behavior together. A bad run proposes mappings that look neat in Kotlin but violate JPA identity, proxy, or loading semantics.
More from jetbrains/skills
spring-kotlin-code-review
Review Kotlin + Spring changes for behavioral regressions, transaction and proxy bugs, API and serialization mistakes, persistence risks, security issues, configuration drift, and missing tests. Use when reviewing a PR, diff, patch, or design change where generic style-focused review would miss Spring-specific correctness and operational risks.
4dependency-conflict-resolver
Diagnose and resolve Gradle and Spring classpath conflicts, version drift, and binary incompatibilities in Kotlin applications. Use when `NoSuchMethodError`, `ClassNotFoundException`, linkage errors, duplicate logging bindings, Jackson or Hibernate mismatches, or BOM-versus-explicit-version conflicts appear, and the fix must respect the repository's real version authorities.
3doc
Use when the task involves reading, creating, or editing `.docx` documents, especially when formatting or layout fidelity matters; prefer `python-docx` plus the bundled `scripts/render_docx.py` for visual checks.
3kotlin-spring-proxy-compatibility
Diagnose and prevent Kotlin plus Spring proxy failures around `@Transactional`, `@Cacheable`, `@Async`, method security, retry, configuration proxies, and JPA entity requirements. Use when AOP annotations appear to do nothing, transactional or cache behavior is inconsistent, compiler plugins may be missing, self-invocation is suspected, or Kotlin final-by-default semantics may break Spring behavior.
3ci-cd-containerization-advisor
Design reproducible build, image, and deployment pipelines for Kotlin plus Spring applications, including CI verification, layered containers, rollout safety, and deployment-time migration coordination. Use when creating or improving Dockerfiles, CI workflows, image hardening, Kubernetes manifests, release gates, or deployment strategies for Spring Boot services, especially where build reproducibility and operational safety matter.
3kotlin-idiomatic-refactorer-spring-aware
Refactor Kotlin code toward clearer, more idiomatic design without breaking Spring behavior, serialization, persistence, or public contracts. Use when Java-flavored Kotlin needs cleanup, domain modeling should become more expressive, or boilerplate should be reduced, but the refactoring must remain safe for proxies, Jackson, JPA, configuration binding, and existing tests.
3