{"id":187,"date":"2023-06-23T21:06:57","date_gmt":"2023-06-23T13:06:57","guid":{"rendered":"http:\/\/blog.ohmmx.com\/?p=187"},"modified":"2023-06-23T21:06:57","modified_gmt":"2023-06-23T13:06:57","slug":"hibernate%e5%91%bd%e5%90%8d%e7%ad%96%e7%95%a5%ef%bc%9a%e9%80%bb%e8%be%91%e5%90%8d%e7%a7%b0","status":"publish","type":"post","link":"http:\/\/blog.ohmmx.com\/?p=187","title":{"rendered":"Hibernate\u547d\u540d\u7b56\u7565\uff1a\u903b\u8f91\u540d\u79f0"},"content":{"rendered":"\n<p>\u63a5\u4e0a\u6587 <a href=\"http:\/\/blog.ohmmx.com\/?p=185\" target=\"_blank\" rel=\"noreferrer noopener\">Hibernate\u547d\u540d\u7b56\u7565\uff1a\u7269\u7406\u540d\u79f0<\/a><\/p>\n\n\n\n<p>\u53c2\u8003\u6587\u7ae0 <a href=\"https:\/\/stackoverflow.com\/questions\/41267416\/hibernate-5-naming-strategy-examples\" target=\"_blank\" rel=\"noreferrer noopener\">Hibernate 5 naming strategy examples<\/a><\/p>\n\n\n\n<p>Spring Boot \u914d\u7f6e\u9879\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>spring.jpa.hibernate.naming.implicit-strategy<\/code><\/pre>\n\n\n\n<p>\u8be5\u914d\u7f6e\u9879\u67095\u4e2a\u503c\uff0c\u89c1\u4e0b\u9762\u7684\u9ed8\u8ba4\u5b9e\u73b0\u7c7b\u548c4\u4e2a\u6269\u5c55\u5b9e\u73b0\u7c7b\u3002<\/p>\n\n\n\n<p>\u4f7f\u7528 PhysicalNamingStrategyStandardImpl\u00a0 \u76f4\u63a5\u6620\u5c04\u8868\u540d\u548c\u5b57\u6bb5\u540d\uff0c\u4e0d\u8fdb\u884c\u9a7c\u5cf0\u8f6c\u6362\u3002<\/p>\n\n\n\n<p>\u903b\u8f91\u540d\u79f0\u5b9e\u73b0\u63a5\u53e3<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>org.hibernate.boot.model.naming.ImplicitNamingStrategy<\/code><\/pre>\n\n\n\n<p>\u9ed8\u8ba4\u5b9e\u73b0\u7c7b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl<\/code><\/pre>\n\n\n\n<p>\u8be5\u5b9e\u73b0\u7c7b\u53c8\u6269\u5c55\u51fa\u4ee5\u4e0b\u51e0\u4e2a\u5b9e\u73b0\u7c7b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl\norg.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyHbmImpl\norg.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl\norg.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy<\/code><\/pre>\n\n\n\n<p>\u5176\u4e2d\u4e09\u4e2a\u662f\u7531Hibernate\u5b9e\u73b0\uff0c\u53e6\u4e00\u4e2a\u7531SpringBoot\u5b9e\u73b0\u3002<\/p>\n\n\n\n<p>\u63a5\u4e0b\u6765\u770b\u770b\u4ed6\u4eec\u4e4b\u95f4\u7684\u533a\u522b\uff1a<\/p>\n\n\n\n<p>\u9996\u5148\u5b9a\u4e49\u4e00\u4e2a\u5d4c\u5165\u5143\u7d20\u7c7b\uff0c\u5176\u4e2d\u4e00\u4e2a\u5b57\u6bb5\u4f7f\u7528 Column \u6ce8\u89e3\u660e\u786e\u5199\u660e\u5b57\u6bb5\uff0c\u53e6\u4e00\u4e2a\u5b57\u6bb5\u4e0d\u4f7f\u7528\u6ce8\u89e3\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Embeddable\r\r\npublic class EmbeddableElement {\r\n\r\n\t@Column(name = \"quoted_field\")\r\n\tprivate String quotedField;\r\n\r\n\tprivate String regularField;\r\n\r\n\t\/\/ getter and setter\r\n}<\/code><\/pre>\n\n\n\n<p>\u5b9a\u4e49\u4e09\u4e2a\u5b9e\u4f53\u7c7b\uff1a<\/p>\n\n\n\n<p>\u57fa\u7840\u8868\u548c\u94fe\u63a5\u8868<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Entity\r\npublic class OwnedEntity {\r\n\t@Id\r\n\tprivate Long id;\r\n\r\n\t@ElementCollection\r\n\t@CollectionTable\r\n\tSet&lt;EmbeddableElement> ownedElements;\r\n\r\n\t\/\/ getter and setter\r\n}<\/code><\/pre>\n\n\n\n<p>\u4e00\u5bf9\u591a\u8868<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Entity\r\n@Table(name = \"mainTable\")\r\npublic class MainEntity {\r\n\t@Id\r\n\tprivate long id;\r\n\r\n\t@ElementCollection\r\n\tprivate Set&lt;EmbeddableElement> mainElements;\r\n\r\n\t@OneToMany(targetEntity = DependentEntity.class)\r\n\tSet&lt;DependentEntity> dependentEntities;\r\n\r\n\t@OneToOne(targetEntity = OwnedEntity.class)\r\n\tOwnedEntity ownedEntity;\r\n\r\n\t\/\/ getter and setter\r\n}<\/code><\/pre>\n\n\n\n<p>\u591a\u5bf9\u4e00\u8868<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Entity\r\n@Table(name = \"dependentTable\")\r\npublic class DependentEntity {\r\n\t@Id\r\n\tprivate long id;\r\n\r\n\t@ManyToOne\r\n\tMainEntity mainEntity;\r\n\r\n\t@ElementCollection\r\n\t@CollectionTable(name = \"dependentElements\")\r\n\tSet&lt;EmbeddableElement> dependentElements;\r\n\r\n\t\/\/ getter and setter\r\n}<\/code><\/pre>\n\n\n\n<p>\u9996\u5148\u770b\u57fa\u7c7b ImplicitNamingStrategyJpaCompliantImpl\uff0c\u5f97\u5230\u5efa\u8868\u8bed\u53e5\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE OwnedEntity (id BIGINT NOT NULL, PRIMARY KEY (id));\r\nCREATE TABLE OwnedEntity_ownedElements (OwnedEntity_id BIGINT NOT NULL, quoted_field VARCHAR(255), regularField VARCHAR(255));\r\n\r\nCREATE TABLE mainTable (id BIGINT NOT NULL, ownedEntity_id BIGINT, PRIMARY KEY (id));\r\nCREATE TABLE MainEntity_mainElements (MainEntity_id BIGINT NOT NULL, quoted_field VARCHAR(255), regularField VARCHAR(255));\r\nCREATE TABLE mainTable_dependentTable (MainEntity_id BIGINT NOT NULL, dependentEntities_id BIGINT NOT NULL, PRIMARY KEY (MainEntity_id, dependentEntities_id));\r\n\r\nCREATE TABLE dependentTable (id BIGINT NOT NULL, mainEntity_id BIGINT, PRIMARY KEY (id));\r\nCREATE TABLE dependentElements (DependentEntity_id BIGINT NOT NULL, quoted_field VARCHAR(255), regularField VARCHAR(255));<\/code><\/pre>\n\n\n\n<p>Hibernate \u539f\u59cb\u547d\u540d\u7b56\u7565 ImplicitNamingStrategyLegacyHbmImpl \uff08\u4ec5\u533a\u522b\u8bed\u53e5\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE mainTable_dependentEntities (MainEntity_id BIGINT NOT NULL, dependentEntities BIGINT NOT NULL, PRIMARY KEY (MainEntity_id, dependentEntities));<\/code><\/pre>\n\n\n\n<p>\u533a\u522b\u5728\u4e8e\u5173\u8054\u8868\u540d\u540e\u7f00\u4e0d\u662f Table \u800c\u662f Entities<\/p>\n\n\n\n<p>JPA\u547d\u540d\u7b56\u7565 ImplicitNamingStrategyLegacyJpaImpl \uff08\u4ec5\u533a\u522b\u8bed\u53e5\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE mainTable_mainElements (mainTable_id BIGINT NOT NULL, quoted_field VARCHAR(255), regularField VARCHAR(255));<\/code><\/pre>\n\n\n\n<p>\u533a\u522b\u5728\u4e8e\u5173\u8054\u8868\u4e3b\u540d\u540e\u7f00\u4e0d\u662f Entity\uff0c\u800c\u662f Table<\/p>\n\n\n\n<p>\u7b56\u7565 ImplicitNamingStrategyComponentPathImpl \uff08\u4ec5\u533a\u522b\u8bed\u53e5\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE MainEntity_mainElements (MainEntity_id BIGINT NOT NULL, quoted_field VARCHAR(255), mainElements_regularField VARCHAR(255));<\/code><\/pre>\n\n\n\n<p>\u533a\u522b\u5728\u4e8e\u5d4c\u5165\u5f0f\u5173\u8054\u8868\u4e2d\u6ca1\u6709\u660e\u786e\u5199\u660e\u5b57\u6bb5\u540d\u7684\u5b57\u6bb5\u4f1a\u6dfb\u52a0\u5c5e\u6027\u540d\u79f0\u524d\u7f00<\/p>\n\n\n\n<p>\u7b56\u7565 SpringImplicitNamingStrategy \uff08\u4ec5\u533a\u522b\u8bed\u53e5\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE mainTable_dependentEntities (MainEntity_id BIGINT NOT NULL, dependentEntities_id BIGINT NOT NULL, PRIMARY KEY (MainEntity_id, dependentEntities_id));<\/code><\/pre>\n\n\n\n<p>\u4e0e Hibernate \u539f\u59cb\u547d\u540d\u7b56\u7565\u7684\u533a\u522b\u4ec5\u5728\u4e8e\u5173\u8054\u8868\u7684\u5b57\u6bb5\u591a\u52a0\u540e\u7f00 _id<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><tbody><tr><td><strong>strategy<\/strong><\/td><td><strong>primaryTable<\/strong><\/td><td><strong>joinTable<\/strong><\/td><td><strong>collectTable<\/strong><\/td><\/tr><tr><td>ImplicitNamingStrategyJpaCompliantImpl<\/td><td>Entity\u7c7b\u540d<\/td><td>Entity\u7269\u7406\u540d\u79f0 + \u5f15\u7528Entity\u7684\u7269\u7406\u540d\u79f0<\/td><td>Entity\u7c7b\u540d + \u5c5e\u6027\u540d\u79f0<\/td><\/tr><tr><td>ImplicitNamingStrategyLegacyHbmImpl<\/td><td>&#8211;<\/td><td>Entity\u7269\u7406\u540d\u79f0 + \u5c5e\u6027\u540d\u79f0<\/td><td>&#8211;<\/td><\/tr><tr><td>ImplicitNamingStrategyLegacyJpaImpl<\/td><td>&#8211;<\/td><td>&#8211;<\/td><td>Entity\u7269\u7406\u540d\u79f0 + \u5c5e\u6027\u540d\u79f0<\/td><\/tr><tr><td>ImplicitNamingStrategyComponentPathImpl<\/td><td>&#8211;<\/td><td>&#8211;<\/td><td>Entity\u5c5e\u6027\u540d\u79f0 + \u5c5e\u6027\u540d\u79f0<\/td><\/tr><tr><td>SpringImplicitNamingStrategy<\/td><td>&#8211;<\/td><td>Entity\u7684\u7269\u7406\u540d\u79f0 + \u5c5e\u6027\u540d\u79f0<\/td><td>&#8211;<\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u63a5\u4e0a\u6587 Hibernate\u547d\u540d\u7b56\u7565\uff1a\u7269\u7406\u540d\u79f0 \u53c2\u8003\u6587\u7ae0 Hibernate 5  &hellip; <a href=\"http:\/\/blog.ohmmx.com\/?p=187\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35,17],"tags":[36,8],"class_list":["post-187","post","type-post","status-publish","format-standard","hentry","category-java","category-17","tag-java","tag-8"],"_links":{"self":[{"href":"http:\/\/blog.ohmmx.com\/index.php?rest_route=\/wp\/v2\/posts\/187","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.ohmmx.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.ohmmx.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.ohmmx.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.ohmmx.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=187"}],"version-history":[{"count":1,"href":"http:\/\/blog.ohmmx.com\/index.php?rest_route=\/wp\/v2\/posts\/187\/revisions"}],"predecessor-version":[{"id":188,"href":"http:\/\/blog.ohmmx.com\/index.php?rest_route=\/wp\/v2\/posts\/187\/revisions\/188"}],"wp:attachment":[{"href":"http:\/\/blog.ohmmx.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=187"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.ohmmx.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=187"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.ohmmx.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=187"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}