kotlin-backend-jpa-entity-mapping

Diagnose and correct Kotlin JPA/Sprint Data entity design and identity issues.

992|38|Updated Mar 13, 2026
One-click install
npx skills add https://github.com/Kotlin/kotlin-agent-skills --skill kotlin-backend-jpa-entity-mapping-kotlin
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: kotlin-backend-jpa-entity-mapping
Source: https://github.com/Kotlin/kotlin-agent-skills/tree/main/skills/kotlin-backend-jpa-entity-mapping
Command: npx skills add https://github.com/Kotlin/kotlin-agent-skills --skill kotlin-backend-jpa-entity-mapping-kotlin

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Model Kotlin persistence code correctly for Spring Data JPA and Hibernate. Covers entity design, identity and equality, uniqueness constraints, relationships, fetch plans, and common ORM (Object-Relational Mapping) traps specific to Kotlin. Use when creating or reviewing JPA (Java Persistence API) entities, diagnosing N+1 or LazyInitializationException, placing indexes and uniqueness rules, or preventing Kotlin-specific bugs such as data class entities and broken equals/hashCode.

Core Features & Use Cases

  • Correct entity design and identity management for Kotlin persistence with JPA/Hibernate.
  • Guidelines to avoid Kotlin data classes for entities and to separate persistence models from DTOs.
  • Practical rules for implementing identity, equality, and uniqueness constraints in real projects.

Quick Start

Define a Kotlin entity as a regular class annotated with @Entity, ensure an ID-based equals/hashCode, and apply the entity-design rules in your Spring Data JPA setup.

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

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

FAQPage Schema
Why does using a Kotlin data class for a JPA entity break equals and hashCode?

Kotlin data classes generate equals and hashCode using all properties, which conflicts with Hibernate entity proxies and lazy loading. Use a regular class with explicit ID-based equality to prevent broken equality checks and ORM trapping issues.

How do I fix LazyInitializationException and N+1 queries in Spring Data JPA?

LazyInitializationException and N+1 queries in Spring Data JPA require safe fetch strategies and proper entity relationship mapping. Apply modern Kotlin JPA best practices by adjusting fetch plans and diagnosing lazy-loading pitfalls in your persistence layer.

What is the best way to implement identity and uniqueness constraints in Kotlin JPA entities?

Identity and uniqueness constraints in Kotlin JPA entities are best implemented using explicit ID-based equality and practical rules for uniqueness. This ensures correct entity design and identity management across typical ORM traps in production code.

How do I define a Kotlin JPA entity correctly for Hibernate?

Define a Kotlin JPA entity as a regular class annotated with @Entity, ensuring an ID-based equals and hashCode implementation. Separate persistence models from DTOs and apply entity-design rules in your Spring Data JPA setup.

Can I use Kotlin data classes for DTOs while keeping regular classes for JPA entities?

Yes, you can use Kotlin data classes for DTOs while keeping regular classes for JPA entities. Separating persistence models from DTOs prevents Kotlin-specific persistence bugs and maintains clean entity design in Spring Data projects.

When should I not use data classes for Hibernate entity mapping in Kotlin?

You should not use data classes for Hibernate entity mapping in Kotlin when dealing with lazy-loaded relationships or complex equality checks. Data classes include all properties in equals and hashCode, breaking ORM proxy behavior and causing entity identity issues.