kotlin-backend-jpa-entity-mapping

Models Kotlin JPA entities with correct identity, equality, and fetch strategies.

Updated Apr 21, 2026
One-click install
npx skills add https://github.com/Zeyad-37/tech-agency --skill kotlin-backend-jpa-entity-mapping-zeyad-37
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: kotlin-backend-jpa-entity-mapping
Source: https://github.com/Zeyad-37/tech-agency/tree/main/.claude/skills/kotlin-backend-jpa-entity-mapping
Command: npx skills add https://github.com/Zeyad-37/tech-agency --skill kotlin-backend-jpa-entity-mapping-zeyad-37

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Kotlin's data class breaks Hibernate's identity semantics, causing corrupted Set/Map membership, detached duplicates, and LazyInitializationException. This Skill provides correct patterns for designing JPA entities in Kotlin so persistence code behaves predictably under dirty tracking, proxying, and concurrent access. ## Core Features & Use Cases - Entity Design Rules: Enforces regular classes over data classes for entities, ID-based equals/hashCode, lazy-safe toString, and no-arg plugin verification. - Uniqueness & Idempotency: Combines database unique constraints with application-level guards for race-safe idempotent operations like reservations. - ORM Trap Diagnosis: Covers N+1 detection via actual query counts, fetch plans with @EntityGraph and JOIN FETCH, bidirectional association maintenance, and bulk update pitfalls. - Use Case: When reviewing a Spring Boot service that silently accumulates duplicate reservations, apply the database constraint plus repository lookup pattern to enforce idempotency at both layers. ## Quick Start Review my Kotlin Spring Data JPA entity classes and fix any data class usage, equality, or lazy loading issues.

Frequently Asked Questions about kotlin-backend-jpa-entity-mapping

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
Why should I not use data class for JPA entities in Kotlin?▼

Data class generates equals/hashCode from all fields, so mutating an entity after adding it to a Set corrupts membership, and Hibernate proxies fail equality checks. Use a regular class with ID-based equality and a constant class-based hashCode instead.

How do I implement equals and hashCode for Kotlin JPA entities?▼

Compare entities by ID only in equals, returning false when the ID is unset, and return a constant like javaClass.hashCode() from hashCode. This keeps Set and Map membership stable across state changes and proxy unwrapping.

How do I fix N+1 queries in Spring Data JPA with Kotlin?▼

Diagnose N+1 by inspecting actual query counts or SQL logs rather than guessing from annotations. Then apply targeted fixes such as @EntityGraph, JOIN FETCH, batch fetching, or DTO projections, and avoid collection fetch joins combined with pagination.

How do I enforce idempotent writes with JPA unique constraints?▼

Declare a UniqueConstraint on the entity table for the business key and add an application-level repository lookup before saving. The application check gives clean errors while the database constraint catches race conditions.

Why does LazyInitializationException happen in Kotlin Spring Boot apps?▼

It occurs when lazy associations are accessed outside a transaction, often triggered by toString, logging, JSON serialization, or DTO mapping. Keep mapping inside a transaction boundary and exclude lazy relations from toString.