kotlin-backend-jpa-entity-mapping

Review Kotlin JPA entities for Spring Data identity and lazy loading issues.

2|Updated Feb 24, 2026
One-click install
npx skills add https://github.com/MajesteitBart/ownkey-keyboard --skill kotlin-backend-jpa-entity-mapping-majesteitbart
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: kotlin-backend-jpa-entity-mapping
Source: https://github.com/MajesteitBart/ownkey-keyboard/tree/main/.agents/skills/kotlin-backend-jpa-entity-mapping
Command: npx skills add https://github.com/MajesteitBart/ownkey-keyboard --skill kotlin-backend-jpa-entity-mapping-majesteitbart

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Kotlin developers often struggle to design JPA entities that work reliably with Hibernate and Spring Data, risking improper equals/hashCode, mutable state pitfalls, and unsafe lazy loading. This skill provides a principled set of rules and patterns to design Kotlin persistence entities that preserve identity, enforce safe fetch plans, and apply correct uniqueness constraints.

Core Features & Use Cases

  • Entity design rules: never use data class for JPA entities; use a regular class; keep DTOs separate.
  • Identity and equality guidance: implement equals/hashCode based on ID; avoid including mutable fields; ensure safe toString.
  • Uniqueness constraints: define database constraints and application guards to prevent duplicates.
  • Query and fetch rules: use appropriate fetch plans and caution with lazy loading.
  • Common ORM traps: bidirectional associations, proxying, and open-in-view considerations.
  • Use case: review and refactor Kotlin JPA entities in a Spring Data project to prevent N+1 and proxies pitfalls.

Quick Start

Review your Kotlin JPA entities in your Spring Data project and apply the rules above to replace data classes with regular entities and implement ID-based identity.

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 avoid using a Kotlin data class for JPA entity design?

JPA entity design requires regular Kotlin classes instead of data classes to prevent Hibernate proxy issues and mutable state pitfalls. Data classes generate flawed equals and hashCode methods that break lazy loading and cause identity bugs in Spring Data persistence.

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

Implement equals and hashCode for Kotlin JPA entities by basing them solely on the entity ID. Avoid including mutable fields to prevent collection corruption and ensure safe proxying when Hibernate loads associations lazily in Spring Data.

How do I prevent N+1 queries and lazy loading pitfalls in Spring Data?

Prevent N+1 queries and lazy loading pitfalls in Spring Data by applying appropriate fetch plans for JPA entity associations. Cautiously manage bidirectional associations and avoid open-in-view to stop unintended proxy initialization outside transactions.

What is the best way to enforce uniqueness constraints in Kotlin JPA entities?

The best way to enforce uniqueness constraints in Kotlin JPA entities is to define explicit database constraints alongside application guardrails. This dual approach prevents duplicates reliably at the persistence layer and catches violations before transaction commit.

Does Spring Data work with Kotlin JPA entities using bidirectional associations?

Spring Data works with Kotlin JPA entities using bidirectional associations, but they introduce common ORM traps. You must carefully manage proxying and fetch plans to avoid lazy loading pitfalls and N+1 queries during entity initialization.