{"enrichment":{"faq":[{"a":"kotlin-backend-jpa-entity-mapping recommends regular classes over data classes for JPA entities because data classes auto-generate equals(), hashCode(), and toString() based on all properties. This breaks entity identity semantics: two entities with identical field values become equal even if they represent different database rows. Data class copy() also creates new instances, corrupting Hibernate's identity map. Use regular classes with explicit, stable equals/hashCode implementations tied to the entity's database identifier instead.","q":"Why are Kotlin data classes problematic for Spring Data JPA entities?"},{"a":"kotlin-backend-jpa-entity-mapping addresses N+1 queries through explicit fetch strategies: use @EntityGraph to eagerly load related entities in a single query, apply fetch = FetchType.EAGER selectively for small collections, or use JOIN FETCH in JPQL. Lazy-load only when necessary and batch-fetch with spring.jpa.properties.hibernate.default_batch_fetch_size. Monitor query logs and prefer DTO projections for read-heavy operations to avoid loading full entity graphs unnecessarily.","q":"How do you fix N+1 query problems in kotlin spring data jpa?"},{"a":"kotlin-backend-jpa-entity-mapping emphasizes that JPA entity identity must be based on the database identifier, not all fields. Override equals() to compare only the @Id field (or composite key), and hashCode() to derive from the same identifier. This ensures entities remain equal across session boundaries and collection operations. Implement equality before the entity is persisted by using a business key or generating IDs early. Never use mutable fields in equals/hashCode calculations.","q":"What's the correct way to implement identity and equality in Kotlin JPA entities?"},{"a":"kotlin-backend-jpa-entity-mapping teaches that bidirectional relationships require careful synchronization: maintain both sides manually in setter methods, use mappedBy on the inverse side to prevent duplicate foreign keys, and apply cascade and orphanRemoval judiciously. Kotlin's nullable types help model optional sides. Avoid lazy-loading the inverse side unless fetched explicitly; use @EntityGraph or JOIN FETCH. Always initialize collections as mutable sets or lists in the constructor to prevent NullPointerException during Hibernate initialization.","q":"How does kotlin-backend-jpa-entity-mapping handle bidirectional relationships?"},{"a":"kotlin-backend-jpa-entity-mapping warns against: using immutable val properties (Hibernate needs mutable fields), relying on default parameter values in constructors (breaks reflection), storing non-entity objects in collections without proper converters, and assuming Hibernate proxies behave like real instances (they fail instanceof checks). Avoid lateinit for lazy-loaded associations; use nullable types instead. Never override equals/hashCode in a way that triggers lazy loading, as this causes LazyInitializationException outside active sessions.","q":"What Kotlin-specific ORM traps should you avoid in entity design?"},{"a":"kotlin-backend-jpa-entity-mapping recommends using @EntityGraph or @NamedEntityGraph to define fetch plans declaratively, ensuring related entities load within the transaction. Apply @Transactional on service methods to keep sessions open during data access. For read-only queries, use DTO projections with constructor expressions to load only needed fields. Configure spring.jpa.open-in-view cautiously\u2014it masks lazy-loading issues. Batch-fetch collections with Hibernate hints to reduce query count while maintaining explicit control over what loads.","q":"How should you optimize fetch plans to prevent lazy initialization exceptions?"}],"shadow_tags":["kotlin-orm","entity-design","persistence-antipatterns","lazy-loading-traps","identity-semantics","fetch-optimization","concurrency-control","constraint-enforcement","proxy-safety","dto-mapping"],"summary_rewrite":"Master Spring Data JPA entity design in Kotlin by learning why regular classes outperform data classes for persistence, how to implement stable identity and equality semantics, and when to enforce uniqueness at both database and application layers. Covers fetch strategies, common ORM traps, and Kotlin-specific patterns for preventing N+1 queries and lazy-loading errors."},"files":[{"bytes":7439,"path":"skills/kotlin-backend-jpa-entity-mapping/SKILL.md","sha256":"5d9c864a55c5ad29f54a807820672a9b5e0360ee33006c63a396a31bef0d8f4e","url":"https://skillfed.io/files/Kotlin/kotlin-agent-skills/kotlin-backend-jpa-entity-mapping/62a53916/SKILL.md"}],"id":"Kotlin/kotlin-agent-skills/kotlin-backend-jpa-entity-mapping","links":{"html":"https://skillfed.io/Kotlin/kotlin-agent-skills/kotlin-backend-jpa-entity-mapping","md":"https://skillfed.io/Kotlin/kotlin-agent-skills/kotlin-backend-jpa-entity-mapping.md","repo":"https://github.com/Kotlin/kotlin-agent-skills"},"meta":{"agents_supported":[],"first_seen":"2026-07-28","forks":37,"language":"Shell","last_updated":"2026-07-21","license":"Apache-2.0","name":"kotlin-backend-jpa-entity-mapping","publisher":"Kotlin","stars":971},"relations":{"similar":[{"id":"decebals/claude-code-java/jpa-patterns"},{"id":"vitorpamplona/amethyst/kotlin-types-value-class"},{"id":"affaan-m/ECC/kotlin-patterns"},{"id":"giuseppe-trisciuoglio/developer-kit/spring-data-jpa"},{"id":"xu-xiang/everything-claude-code-zh/jpa-patterns"},{"id":"dawiddutoit/custom-claude/java-spring-service"},{"id":"chrisbanes/skills/kotlin-types-value-class"},{"id":"pluginagentmarketplace/custom-plugin-java/java-jpa-hibernate"},{"id":"doccker/cc-use-exp/java-dev"},{"id":"affaan-m/ECC/jpa-patterns"}]},"slug":{"owner":"Kotlin","repo":"kotlin-agent-skills","skill":"kotlin-backend-jpa-entity-mapping"},"version":"62a53916"}
