{"id":360578,"date":"2024-05-21T00:56:02","date_gmt":"2024-05-21T00:56:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=360578"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=360578","title":{"rendered":"<span>\u041f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u043b\u0438 data class \u0434\u043b\u044f JPA entity?<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<h2>tldr;<\/h2>\n<blockquote>\n<p><em>\u0423 \u0442\u0435\u0431\u044f \u043a\u043b\u0430\u0441\u0441 \u0431\u0435\u0437 data!<\/em><\/p>\n<p><em>\u0414\u0430, \u043d\u0435\u043f\u043b\u043e\u0445\u043e\u0439 \u0442\u0430\u043a\u043e\u0439 \u043a\u043b\u0430\u0441\u0441<\/em><\/p>\n<\/blockquote>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c data class \u0434\u043b\u044f JPA \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u043e\u043f\u0440\u0430\u0432\u0434\u0430\u043d\u043e, \u0435\u0441\u043b\u0438 id \u0437\u0430\u043f\u0438\u0441\u0438 \u0433\u0435\u043d\u0435\u0440\u0438\u0442\u0441\u044f \u043d\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0438 \u0438\u0437\u0431\u044b\u0442\u043e\u0447\u043d\u043e, \u0435\u0441\u043b\u0438 id \u0433\u0435\u043d\u0435\u0440\u0438\u0442\u0441\u044f \u043d\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445, \u0442\u0430\u043a \u043a\u0430\u043a \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u043c\u0435\u0442\u043e\u0434\u044b equals \u0438 hashcode.<\/p>\n<h4>\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0441\u0442\u0438<\/h4>\n<p>\u0415\u0441\u0442\u044c \u043a\u043b\u0430\u0441\u0441, \u043e\u0442\u043c\u0435\u0447\u0435\u043d\u043d\u044b\u0439 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0435\u0439 @Entity \u0438 @Table. \u041d\u0443\u0436\u043d\u043e \u043b\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c data \u043f\u0435\u0440\u0435\u0434 class?<\/p>\n<pre><code class=\"kotlin\">@Table @Entity class Entity(    @Id    val id: SomeIdentityType,    @Column    val name: String )<\/code><\/pre>\n<p>\u041e\u0442\u0432\u0435\u0442 \u043d\u0430 \u0432\u043e\u043f\u0440\u043e\u0441 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0442\u043e\u0433\u043e, \u0433\u0434\u0435 \u0433\u0435\u043d\u0435\u0440\u0438\u0442\u0441\u044f id, \u043d\u043e \u0432\u043d\u0430\u0447\u0430\u043b\u0435 \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c\u0441\u044f \u0441 data class. Data class \u0431\u044b\u043b\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u044b \u0434\u043b\u044f \u0442\u0435\u0445, \u043a\u043e\u043c\u0443 \u043b\u0435\u043d\u044c \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u043c\u0435\u0442\u043e\u0434\u044b. \u041d\u0438\u0436\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u043d data class \u0438 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d bytecode.<\/p>\n<pre><code class=\"kotlin\">data class D(val name: String)<\/code><\/pre>\n<p>\u0418\u0437 bytecode \u044f \u0443\u0434\u0430\u043b\u0438\u043b &#171;\u043b\u0438\u0448\u043d\u0435\u0435&#187; \u0438 \u043e\u0441\u0442\u0430\u0432\u0438\u043b \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0435 \u0434\u043b\u044f \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0438\u0445 \u0440\u0430\u0441\u0441\u0443\u0436\u0434\u0435\u043d\u0438\u0439.<\/p>\n<pre><code class=\"java\">public static final class D {    @NotNull    private final String name;     @NotNull    public final String getName() {       return this.name;    }     @NotNull    public String toString() {       return \"D(name=\" + this.name + \")\";    }     public int hashCode() {       String var10000 = this.name;       return var10000 != null ? var10000.hashCode() : 0;    }     public boolean equals(@Nullable Object var1) {       if (this != var1) {          if (var1 instanceof Scratch_7.D) {             Scratch_7.D var2 = (Scratch_7.D)var1;             if (Intrinsics.areEqual(this.name, var2.name)) {                return true;             }          }           return false;       } else {          return true;       }    } }<\/code><\/pre>\n<p>\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, data \u043f\u0435\u0440\u0435\u0434 class \u044d\u0442\u043e \u0443\u043a\u0430\u0437\u0430\u043d\u0438\u0435 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0443 \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043c\u0435\u0442\u043e\u0434\u044b toString(), equals(), hashCode() \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u0438\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u043e\u043b\u0435\u0439 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430.<\/p>\n<p>JPA Entity \u044d\u0442\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u044e\u0442 \u043f\u043e\u043b\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u043d\u0430 \u0441\u0442\u0440\u043e\u043a\u0438 \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445. \u041d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u0432\u0437\u0433\u043b\u044f\u0434 data \u043a\u043b\u0430\u0441\u0441 \u044d\u0442\u043e \u0442\u043e, \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e, \u043d\u043e \u0442\u0443\u0442 \u0435\u0441\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043c\u043e\u043c\u0435\u043d\u0442\u043e\u0432 (\u043d\u0435 \u043d\u044e\u0430\u043d\u0441\u043e\u0432). \u041f\u043e \u0441\u0443\u0442\u0438 \u0432\u0441\u0435 \u043a\u0440\u0443\u0442\u0438\u0442\u0441\u044f \u0432\u043e\u043a\u0440\u0443\u0433 \u043f\u043e\u043b\u044f, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0435\u0442\u0441\u044f \u043d\u0430 &#171;\u043f\u0435\u0440\u0432\u0438\u0447\u043d\u044b\u0439 \u043a\u043b\u044e\u0447&#187; (id). Id \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d \u043a\u0430\u043a \u0441\u043e \u0441\u0442\u043e\u0440\u043e\u043d\u044b \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u0442\u0430\u043a \u0438 \u0441\u043e \u0441\u0442\u043e\u0440\u043e\u043d\u044b \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445. \u0412 \u043f\u0435\u0440\u0432\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0443\u0436\u0435\u043d \u0442\u0438\u043f, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0434\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u043e \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u0446\u0438\u0440\u0443\u0435\u0442 \u0437\u0430\u043f\u0438\u0441\u044c \u0438 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u043c \u0442\u0430\u043a\u043e\u0433\u043e \u0442\u0438\u043f\u0430 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c UUID.<\/p>\n<pre><code class=\"kotlin\">@Table @Entity data class EntityApp(     @Id     val id: UUID,     @Column     var name: String )  @Test fun `test app generated id`() {     val name = \"\u0438\u043c\u044f\"     val id = UUID.randomUUID()     val (entity1, entity2) = EntityApp(id, name) to EntityApp(id, name)      val m = mutableMapOf&lt;EntityApp, String>()     m[entity1] = \"\u041d\u0430\u0439\u0434\u0435\u043d\"      println(\"\u0414\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0432 \u0431.\u0434.\")     println(\"${entity1 == entity2}\") \/\/ true      val result = entityAppRepository.save(entity1)      println(\"\u041f\u043e\u0441\u043b\u0435 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0432 \u0431.\u0434.\")     println(\"${entity1 == result}\") \/\/ true     println(\"${entity1 == entity2}\") \/\/ true     println(m[entity1] ?: \"\u041d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\") \/\/ \u041d\u0430\u0439\u0434\u0435\u043d }<\/code><\/pre>\n<p>\u041f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c data \u043f\u0435\u0440\u0435\u0434 class \u0434\u043b\u044f JPA Entity \u043e\u043f\u0440\u0430\u0432\u0434\u0430\u043d\u043e, \u0435\u0441\u043b\u0438 id \u0433\u0435\u043d\u0435\u0440\u0438\u0442\u0441\u044f \u043d\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u043b\u0443\u0447\u0430\u0439, \u043a\u043e\u0433\u0434\u0430 id \u0433\u0435\u043d\u0435\u0440\u0438\u0442\u0441\u044f \u043d\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<pre><code class=\"kotlin\">@Table @Entity data class EntityDb(     @Id     @GeneratedValue(strategy = GenerationType.IDENTITY)     val id: Long? = null,     @Column     var name: String )  @Test fun `test db generated id`() {     val name = \"\u0438\u043c\u044f\"     val (entityDb1, entityDb2) = EntityDb(name = name) to EntityDb(name = name)          val m = mutableMapOf&lt;EntityDb, String>()     m[entityDb1] = \"\u041d\u0430\u0439\u0434\u0435\u043d\"      println(\"\u0414\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0432 \u0431.\u0434.\")     println(\"${entityDb1 == entityDb2}\") \/\/ true      val result = entityDbRepository.save(entityDb1)      println(\"\u041f\u043e\u0441\u043b\u0435 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0432 \u0431.\u0434.\")     println(\"${entityDb1 == result}\") \/\/ true     println(\"${entityDb1 == entityDb2}\") \/\/ false     println(m[entityDb1] ?: \"\u041d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\") \/\/ \u041d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d }<\/code><\/pre>\n<p>\u041e\u0431\u044a\u0435\u043a\u0442 \u043e\u043a\u0430\u0437\u0430\u043b\u0441\u044f \u0438\u0437\u043c\u0435\u043d\u0451\u043d \u043f\u043e\u0441\u043b\u0435 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0438 \u043f\u043e\u044d\u0442\u043e\u043c\u0443 entityDb1 != entityDb2. \u0410\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 equals \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u043b \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u043d\u0430 id == other.id. \u0414\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f id \u0431\u044b\u043b null, \u0430 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u0438\u043d\u044f\u043b \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0440\u0430\u0432\u043d\u043e\u0435 id \u0438\u0437 \u0431\u0430\u0437\u044b. entityDb1 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d \u0432 hashmap, \u043d\u043e \u043f\u043e\u0441\u043b\u0435 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0432 \u0431.\u0434. hashcode \u0438\u0437\u043c\u0435\u043d\u0438\u043b\u0441\u044f (\u0442\u0430\u043a \u043a\u0430\u043a \u0438\u0437\u043c\u0435\u043d\u0438\u043b\u0441\u044f id) \u0438 \u0442\u0435\u043f\u0435\u0440\u044c entityDb1 \u043d\u0435 \u043d\u0430\u0439\u0442\u0438 \u0432 hashmap \u043f\u043e\u0441\u043b\u0435 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f. \u041e\u0431\u0430 \u043c\u0435\u0442\u043e\u0434\u0430 \u0442\u0440\u0435\u0431\u0443\u044e\u0442 \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0438 \u0443\u043a\u0430\u0437\u0430\u043d\u0438\u0435 data class \u0434\u043b\u044f JPA Entity \u0438\u0437\u0431\u044b\u0442\u043e\u0447\u043d\u043e. \u041a\u0441\u0442\u0430\u0442\u0438, \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 toString \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u043e \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u0442 side \u044d\u0444\u0444\u0435\u043a\u0442, \u043a\u043e\u0433\u0434\u0430 id \u0434\u043b\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u043d\u0435 \u0437\u0430\u0434\u0430\u043d \u0434\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0432 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 id \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e\u0441\u043b\u0435 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0432 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c data class \u0434\u043b\u044f JPA \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u043e\u043f\u0440\u0430\u0432\u0434\u0430\u043d\u043e, \u0435\u0441\u043b\u0438 id \u0437\u0430\u043f\u0438\u0441\u0438 \u0433\u0435\u043d\u0435\u0440\u0438\u0442\u0441\u044f \u043d\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0438 \u0438\u0437\u0431\u044b\u0442\u043e\u0447\u043d\u043e, \u0435\u0441\u043b\u0438 id \u0433\u0435\u043d\u0435\u0440\u0438\u0442\u0441\u044f \u043d\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445, \u0442\u0430\u043a \u043a\u0430\u043a \u043f\u0440\u0438\u0434\u0451\u0442\u0441\u044f \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u043c\u0435\u0442\u043e\u0434\u044b equals \u0438 hashcode.<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/774916\/\"> https:\/\/habr.com\/ru\/articles\/774916\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<h2>tldr;<\/h2>\n<blockquote>\n<p><em>\u0423 \u0442\u0435\u0431\u044f \u043a\u043b\u0430\u0441\u0441 \u0431\u0435\u0437 data!<\/em><\/p>\n<p><em>\u0414\u0430, \u043d\u0435\u043f\u043b\u043e\u0445\u043e\u0439 \u0442\u0430\u043a\u043e\u0439 \u043a\u043b\u0430\u0441\u0441<\/em><\/p>\n<\/blockquote>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c data class \u0434\u043b\u044f JPA \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u043e\u043f\u0440\u0430\u0432\u0434\u0430\u043d\u043e, \u0435\u0441\u043b\u0438 id \u0437\u0430\u043f\u0438\u0441\u0438 \u0433\u0435\u043d\u0435\u0440\u0438\u0442\u0441\u044f \u043d\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0438 \u0438\u0437\u0431\u044b\u0442\u043e\u0447\u043d\u043e, \u0435\u0441\u043b\u0438 id \u0433\u0435\u043d\u0435\u0440\u0438\u0442\u0441\u044f \u043d\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445, \u0442\u0430\u043a \u043a\u0430\u043a \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u043c\u0435\u0442\u043e\u0434\u044b equals \u0438 hashcode.<\/p>\n<h4>\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0441\u0442\u0438<\/h4>\n<p>\u0415\u0441\u0442\u044c \u043a\u043b\u0430\u0441\u0441, \u043e\u0442\u043c\u0435\u0447\u0435\u043d\u043d\u044b\u0439 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0435\u0439 @Entity \u0438 @Table. \u041d\u0443\u0436\u043d\u043e \u043b\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c data \u043f\u0435\u0440\u0435\u0434 class?<\/p>\n<pre><code class=\"kotlin\">@Table @Entity class Entity(    @Id    val id: SomeIdentityType,    @Column    val name: String )<\/code><\/pre>\n<p>\u041e\u0442\u0432\u0435\u0442 \u043d\u0430 \u0432\u043e\u043f\u0440\u043e\u0441 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0442\u043e\u0433\u043e, \u0433\u0434\u0435 \u0433\u0435\u043d\u0435\u0440\u0438\u0442\u0441\u044f id, \u043d\u043e \u0432\u043d\u0430\u0447\u0430\u043b\u0435 \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c\u0441\u044f \u0441 data class. Data class \u0431\u044b\u043b\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u044b \u0434\u043b\u044f \u0442\u0435\u0445, \u043a\u043e\u043c\u0443 \u043b\u0435\u043d\u044c \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u043c\u0435\u0442\u043e\u0434\u044b. \u041d\u0438\u0436\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u043d data class \u0438 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d bytecode.<\/p>\n<pre><code class=\"kotlin\">data class D(val name: String)<\/code><\/pre>\n<p>\u0418\u0437 bytecode \u044f \u0443\u0434\u0430\u043b\u0438\u043b &#171;\u043b\u0438\u0448\u043d\u0435\u0435&#187; \u0438 \u043e\u0441\u0442\u0430\u0432\u0438\u043b \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0435 \u0434\u043b\u044f \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0438\u0445 \u0440\u0430\u0441\u0441\u0443\u0436\u0434\u0435\u043d\u0438\u0439.<\/p>\n<pre><code class=\"java\">public static final class D {    @NotNull    private final String name;     @NotNull    public final String getName() {       return this.name;    }     @NotNull    public String toString() {       return \"D(name=\" + this.name + \")\";    }     public int hashCode() {       String var10000 = this.name;       return var10000 != null ? var10000.hashCode() : 0;    }     public boolean equals(@Nullable Object var1) {       if (this != var1) {          if (var1 instanceof Scratch_7.D) {             Scratch_7.D var2 = (Scratch_7.D)var1;             if (Intrinsics.areEqual(this.name, var2.name)) {                return true;             }          }           return false;       } else {          return true;       }    } }<\/code><\/pre>\n<p>\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, data \u043f\u0435\u0440\u0435\u0434 class \u044d\u0442\u043e \u0443\u043a\u0430\u0437\u0430\u043d\u0438\u0435 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0443 \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043c\u0435\u0442\u043e\u0434\u044b toString(), equals(), hashCode() \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u0438\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u043e\u043b\u0435\u0439 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430.<\/p>\n<p>JPA Entity \u044d\u0442\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u044e\u0442 \u043f\u043e\u043b\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u043d\u0430 \u0441\u0442\u0440\u043e\u043a\u0438 \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445. \u041d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u0432\u0437\u0433\u043b\u044f\u0434 data \u043a\u043b\u0430\u0441\u0441 \u044d\u0442\u043e \u0442\u043e, \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e, \u043d\u043e \u0442\u0443\u0442 \u0435\u0441\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043c\u043e\u043c\u0435\u043d\u0442\u043e\u0432 (\u043d\u0435 \u043d\u044e\u0430\u043d\u0441\u043e\u0432). \u041f\u043e \u0441\u0443\u0442\u0438 \u0432\u0441\u0435 \u043a\u0440\u0443\u0442\u0438\u0442\u0441\u044f \u0432\u043e\u043a\u0440\u0443\u0433 \u043f\u043e\u043b\u044f, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0435\u0442\u0441\u044f \u043d\u0430 &#171;\u043f\u0435\u0440\u0432\u0438\u0447\u043d\u044b\u0439 \u043a\u043b\u044e\u0447&#187; (id). Id \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d \u043a\u0430\u043a \u0441\u043e \u0441\u0442\u043e\u0440\u043e\u043d\u044b \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u0442\u0430\u043a \u0438 \u0441\u043e \u0441\u0442\u043e\u0440\u043e\u043d\u044b \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445. \u0412 \u043f\u0435\u0440\u0432\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0443\u0436\u0435\u043d \u0442\u0438\u043f, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0434\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u043e \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u0446\u0438\u0440\u0443\u0435\u0442 \u0437\u0430\u043f\u0438\u0441\u044c \u0438 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u043c \u0442\u0430\u043a\u043e\u0433\u043e \u0442\u0438\u043f\u0430 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c UUID.<\/p>\n<pre><code class=\"kotlin\">@Table @Entity data class EntityApp(     @Id     val id: UUID,     @Column     var name: String )  @Test fun `test app generated id`() {     val name = \"\u0438\u043c\u044f\"     val id = UUID.randomUUID()     val (entity1, entity2) = EntityApp(id, name) to EntityApp(id, name)      val m = mutableMapOf&lt;EntityApp, String>()     m[entity1] = \"\u041d\u0430\u0439\u0434\u0435\u043d\"      println(\"\u0414\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0432 \u0431.\u0434.\")     println(\"${entity1 == entity2}\") \/\/ true      val result = entityAppRepository.save(entity1)      println(\"\u041f\u043e\u0441\u043b\u0435 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0432 \u0431.\u0434.\")     println(\"${entity1 == result}\") \/\/ true     println(\"${entity1 == entity2}\") \/\/ true     println(m[entity1] ?: \"\u041d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\") \/\/ \u041d\u0430\u0439\u0434\u0435\u043d }<\/code><\/pre>\n<p>\u041f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c data \u043f\u0435\u0440\u0435\u0434 class \u0434\u043b\u044f JPA Entity \u043e\u043f\u0440\u0430\u0432\u0434\u0430\u043d\u043e, \u0435\u0441\u043b\u0438 id \u0433\u0435\u043d\u0435\u0440\u0438\u0442\u0441\u044f \u043d\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u043b\u0443\u0447\u0430\u0439, \u043a\u043e\u0433\u0434\u0430 id \u0433\u0435\u043d\u0435\u0440\u0438\u0442\u0441\u044f \u043d\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<pre><code class=\"kotlin\">@Table @Entity data class EntityDb(     @Id     @GeneratedValue(strategy = GenerationType.IDENTITY)     val id: Long? = null,     @Column     var name: String )  @Test fun `test db generated id`() {     val name = \"\u0438\u043c\u044f\"     val (entityDb1, entityDb2) = EntityDb(name = name) to EntityDb(name = name)          val m = mutableMapOf&lt;EntityDb, String>()     m[entityDb1] = \"\u041d\u0430\u0439\u0434\u0435\u043d\"      println(\"\u0414\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0432 \u0431.\u0434.\")     println(\"${entityDb1 == entityDb2}\") \/\/ true      val result = entityDbRepository.save(entityDb1)      println(\"\u041f\u043e\u0441\u043b\u0435 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0432 \u0431.\u0434.\")     println(\"${entityDb1 == result}\") \/\/ true     println(\"${entityDb1 == entityDb2}\") \/\/ false     println(m[entityDb1] ?: \"\u041d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\") \/\/ \u041d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d }<\/code><\/pre>\n<p>\u041e\u0431\u044a\u0435\u043a\u0442 \u043e\u043a\u0430\u0437\u0430\u043b\u0441\u044f \u0438\u0437\u043c\u0435\u043d\u0451\u043d \u043f\u043e\u0441\u043b\u0435 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0438 \u043f\u043e\u044d\u0442\u043e\u043c\u0443 entityDb1 != entityDb2. \u0410\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 equals \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u043b \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u043d\u0430 id == other.id. \u0414\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f id \u0431\u044b\u043b null, \u0430 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u0438\u043d\u044f\u043b \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0440\u0430\u0432\u043d\u043e\u0435 id \u0438\u0437 \u0431\u0430\u0437\u044b. entityDb1 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d \u0432 hashmap, \u043d\u043e \u043f\u043e\u0441\u043b\u0435 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0432 \u0431.\u0434. hashcode \u0438\u0437\u043c\u0435\u043d\u0438\u043b\u0441\u044f (\u0442\u0430\u043a \u043a\u0430\u043a \u0438\u0437\u043c\u0435\u043d\u0438\u043b\u0441\u044f id) \u0438 \u0442\u0435\u043f\u0435\u0440\u044c entityDb1 \u043d\u0435 \u043d\u0430\u0439\u0442\u0438 \u0432 hashmap \u043f\u043e\u0441\u043b\u0435 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f. \u041e\u0431\u0430 \u043c\u0435\u0442\u043e\u0434\u0430 \u0442\u0440\u0435\u0431\u0443\u044e\u0442 \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0438 \u0443\u043a\u0430\u0437\u0430\u043d\u0438\u0435 data class \u0434\u043b\u044f JPA Entity \u0438\u0437\u0431\u044b\u0442\u043e\u0447\u043d\u043e. \u041a\u0441\u0442\u0430\u0442\u0438, \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 toString \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u043e \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u0442 side \u044d\u0444\u0444\u0435\u043a\u0442, \u043a\u043e\u0433\u0434\u0430 id \u0434\u043b\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u043d\u0435 \u0437\u0430\u0434\u0430\u043d \u0434\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0432 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 id \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e\u0441\u043b\u0435 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0432 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c data class \u0434\u043b\u044f JPA \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u043e\u043f\u0440\u0430\u0432\u0434\u0430\u043d\u043e, \u0435\u0441\u043b\u0438 id \u0437\u0430\u043f\u0438\u0441\u0438 \u0433\u0435\u043d\u0435\u0440\u0438\u0442\u0441\u044f \u043d\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0438 \u0438\u0437\u0431\u044b\u0442\u043e\u0447\u043d\u043e, \u0435\u0441\u043b\u0438 id \u0433\u0435\u043d\u0435\u0440\u0438\u0442\u0441\u044f \u043d\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445, \u0442\u0430\u043a \u043a\u0430\u043a \u043f\u0440\u0438\u0434\u0451\u0442\u0441\u044f \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u043c\u0435\u0442\u043e\u0434\u044b equals \u0438 hashcode.<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/774916\/\"> https:\/\/habr.com\/ru\/articles\/774916\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-360578","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/360578","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=360578"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/360578\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=360578"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=360578"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=360578"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}