{"id":3660,"date":"2019-01-02T11:24:07","date_gmt":"2019-01-02T03:24:07","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=3660"},"modified":"2019-01-02T11:24:07","modified_gmt":"2019-01-02T03:24:07","slug":"%e8%af%a6%e8%a7%a3-entity-%e5%92%8c-table-%e6%b3%a8%e8%a7%a3%e7%9a%84%e7%94%a8%e6%b3%95","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=3660","title":{"rendered":"\u8be6\u89e3 @Entity \u548c @Table \u6ce8\u89e3\u7684\u7528\u6cd5"},"content":{"rendered":"<p>\n\t\u6700\u8fd1\u770b\u4e86\u5f88\u591a\u6587\u7ae0\u90fd\u8bf4\uff0c@Entity \u548c @Table \u6ce8\u89e3\u662f Hibernate \u4e2d\u7684\u6ce8\u89e3\uff0c\u751a\u81f3\u4e00\u4e9b\u6587\u7ae0\u7684\u6807\u9898\u90fd\u76f4\u63a5\u8fd9\u6837\u5199\u3002\u6211\u90fd\u4e0d\u77e5\u9053\u4ed6\u4eec\u8fd9\u6837\u8bef\u5bfc\u4e86\u591a\u5c11\u4eba\uff0c\u4eca\u5929\u6211\u5c31\u6765\u7ed9\u5927\u5bb6\u7ea0\u6b63\u4e00\u4e0b\uff01\n<\/p>\n<p>\n\t@Entity \u6ce8\u89e3\u548c @Table \u6ce8\u89e3\u90fd\u662f Java Persistence API \u4e2d\u5b9a\u4e49\u7684\u4e00\u79cd\u6ce8\u89e3\u3002\u4f60\u8bf4\u5b83\u662f jpa\u3001hibernate\u3001Spring \u7b49\u4e2d\u7684\u6ce8\u89e3\u90fd\u4e0d\u592a\u51c6\u786e\u3002@Entity \u548c @Table \u6ce8\u89e3\u90fd\u5fc5\u987b\u9075\u5faa Java Persistence API \u4e2d\u5b9a\u4e49\u7684\u4e00\u79cd\u67e5\u8be2\u8bed\u8a00\uff08JPQL\uff09\u3002\n<\/p>\n<p>\n\t@Entity \u548c @Table \u662f JDK1.5 \u4ee5\u540e\u652f\u6301\u7684\u5143\u6570\u636e\u6ce8\u89e3\uff08Annotation\uff09\u3002\n<\/p>\n<p>\n\t@Entity \u6ce8\u89e3\u6e90\u7801\u5982\u4e0b\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\npackage javax.persistence;\r\n@java.lang.annotation.Documented\r\n@java.lang.annotation.Target({java.lang.annotation.ElementType.TYPE})\r\n@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)\r\npublic @interface Entity {\r\n    java.lang.String name() default &quot;&quot;;\r\n}<\/pre>\n<p>\n\t@Table \u6ce8\u89e3\u6e90\u7801\u5982\u4e0b\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\npackage javax.persistence;\r\n@java.lang.annotation.Target({java.lang.annotation.ElementType.TYPE})\r\n@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)\r\npublic @interface Table {\r\n    java.lang.String name() default &quot;&quot;;\r\n    java.lang.String catalog() default &quot;&quot;;\r\n    java.lang.String schema() default &quot;&quot;;\r\n    javax.persistence.UniqueConstraint[] uniqueConstraints() default {};\r\n    javax.persistence.Index[] indexes() default {};\r\n}<\/pre>\n<p>\n\t@Entity \u8bf4\u660e\u8fd9\u4e2a class \u662f\u5b9e\u4f53\u7c7b\uff0c\u5e76\u4e14\u4f7f\u7528\u9ed8\u8ba4\u7684 orm \u89c4\u5219\uff0c\u5373 class \u540d\u5373\u6570\u636e\u5e93\u8868\u4e2d\u8868\u540d\uff0cclass \u5b57\u6bb5\u540d\u5373\u8868\u4e2d\u7684\u5b57\u6bb5\u540d\u3002@Entity \u6ce8\u89e3\u6307\u540d\u8fd9\u662f\u4e00\u4e2a\u5b9e\u4f53 Bean\u3002\u4e0b\u9762\u6211\u4eec\u770b\u4e00\u4e2a\u4f8b\u5b50\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\n@Entity \r\npublic class Employee implements Serializable { \r\n    private static final long serialVersionUID = 1L; \r\n    @Id \r\n    private Long id; \r\n    private String name; \r\n    private int age; \r\n    private String addree; \r\n   \/\/ Getters and Setters \r\n}<\/pre>\n<p>\n\t\u5982\u679c\u6ca1\u6709 @javax.persistence.Entity \u548c @javax.persistence.Id \u8fd9\u4e24\u4e2a\u6ce8\u89e3\u7684\u8bdd\uff0c\u5b83\u5b8c\u5168\u5c31\u662f\u4e00\u4e2a\u5178\u578b\u7684 POJO \u7684 Java \u7c7b\uff0c\u73b0\u5728\u52a0\u4e0a\u8fd9\u4e24\u4e2a\u6ce8\u89e3\u4e4b\u540e\uff0c\u5c31\u53ef\u4ee5\u4f5c\u4e3a\u4e00\u4e2a\u5b9e\u4f53\u7c7b\u4e0e\u6570\u636e\u5e93\u4e2d\u7684\u8868\u76f8\u5bf9\u5e94\u3002\u4ed6\u5728\u6570\u636e\u5e93\u4e2d\u7684\u5bf9\u5e94\u7684\u8868\u4e3a\uff1a\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"@Entity \u6ce8\u89e3 ER \u56fe\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/wx1.sinaimg.cn\/large\/8fa5dcfcgy1fys1pb3icmj20bz04paa8.jpg\" style=\"width: 431px;height: 169px\" \/>\n<\/p>\n<p>\n\t\u5bf9\u5e94\u7684\u6620\u5c04\u89c4\u5219\u4e3a\uff1a\n<\/p>\n<ul>\n<li>\n<p>\n\t\t\t\u5b9e\u4f53\u7c7b\u5fc5\u987b\u7528 @javax.persistence.Entity \u8fdb\u884c\u6ce8\u89e3\uff1b\u5982\u679c\u4e0d\u7528\u8fd9\u4e2a\u6ce8\u89e3\u4f1a\u62a5 org.springframework.beans.factory.BeanCreationException: Error creating bean with name &#039;entityManagerFactory&#039; defined in class path resource [org\/springframework\/boot\/autoconfigure\/orm\/jpa\/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: @OneToOne or @ManyToOne \u5f02\u5e38\n\t\t<\/p>\n<\/li>\n<li>\n<p>\n\t\t\t\u5fc5\u987b\u4f7f\u7528 @javax.persistence.Id \u6765\u6ce8\u89e3\u4e00\u4e2a\u4e3b\u952e\uff1b\u5982\u679c\u6ca1\u6709\u8fd9\u4e2a\u6ce8\u89e3\u4f1a\u62a5\uff1aorg.springframework.beans.factory.BeanCreationException: Error creating bean with name &#039;entityManagerFactory&#039; defined in class path resource [org\/springframework\/boot\/autoconfigure\/orm\/jpa\/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: No identifier specified for entity \u5f02\u5e38\n\t\t<\/p>\n<\/li>\n<li>\n<p>\n\t\t\t\u5b9e\u4f53\u7c7b\u5fc5\u987b\u62e5\u6709\u4e00\u4e2a public \u6216\u8005 protected \u7684\u65e0\u53c2\u6784\u9020\u51fd\u6570\uff0c\u4e4b\u5916\u5b9e\u4f53\u7c7b\u8fd8\u53ef\u4ee5\u62e5\u6709\u5176\u4ed6\u7684\u6784\u9020\u51fd\u6570\uff1b\u5982\u679c\u6ca1\u6709\u65e0\u53c2\u6784\u9020\u51fd\u6570\u4f1a\u62a5 org.springframework.orm.jpa.JpaSystemException: No default constructor for entity: &nbsp;: com.xttblog.entity.XttblogTest; nested exception is org.hibernate.InstantiationException: No default constructor for entity: &nbsp;: com.xttblog.entity.XttblogTest \u5f02\u5e38\n\t\t<\/p>\n<\/li>\n<li>\n<p>\n\t\t\t\u5b9e\u4f53\u7c7b\u5fc5\u987b\u662f\u4e00\u4e2a\u9876\u7ea7\u7c7b\uff08top-level class\uff09\u3002\u4e00\u4e2a\u679a\u4e3e\uff08enum\uff09\u6216\u8005\u4e00\u4e2a\u63a5\u53e3\uff08interface\uff09\u4e0d\u80fd\u88ab\u6ce8\u89e3\u4e3a\u4e00\u4e2a\u5b9e\u4f53\uff1b\n\t\t<\/p>\n<\/li>\n<li>\n<p>\n\t\t\t\u5b9e\u4f53\u7c7b\u4e0d\u80fd\u662f final \u7c7b\u578b\u7684\uff0c\u4e5f\u4e0d\u80fd\u6709 final \u7c7b\u578b\u7684\u65b9\u6cd5\uff1b\n\t\t<\/p>\n<\/li>\n<li>\n<p>\n\t\t\t\u5982\u679c\u5b9e\u4f53\u7c7b\u7684\u4e00\u4e2a\u5b9e\u4f8b\u9700\u8981\u7528\u4f20\u503c\u7684\u65b9\u5f0f\u8c03\u7528\uff08\u4f8b\u5982\uff0c\u8fdc\u7a0b\u8c03\u7528\uff09\uff0c\u5219\u8fd9\u4e2a\u5b9e\u4f53\u7c7b\u5fc5\u987b\u5b9e\u73b0\uff08implements\uff09java.io.Serializable \u63a5\u53e3\u3002\n\t\t<\/p>\n<\/li>\n<\/ul>\n<p>\n\t@Table \u6ce8\u89e3\u662f\u4e00\u4e2a\u975e\u5fc5\u987b\u7684\u6ce8\u89e3\u3002@Table \u6ce8\u89e3\u6307\u5b9a\u4e86 Entity \u6240\u8981\u6620\u5c04\u5e26\u6570\u636e\u5e93\u8868\uff0c\u5176\u4e2d @Table.name() \u7528\u6765\u6307\u5b9a\u6620\u5c04\u8868\u7684\u8868\u540d\u3002\u58f0\u660e\u6b64\u5bf9\u8c61\u6620\u5c04\u5230\u6570\u636e\u5e93\u7684\u6570\u636e\u8868\uff0c\u901a\u8fc7\u5b83\u53ef\u4ee5\u4e3a\u5b9e\u4f53\u6307\u5b9a\u8868(talbe),\u76ee\u5f55 (Catalog) \u548c schema \u7684\u540d\u5b57\u3002\n<\/p>\n<p>\n\t\u5982\u679c\u540c\u65f6\u4f7f\u7528\u4e86 @Entity(name=&quot;student&quot;) \u548c @Table(name=&quot;students&quot;)\uff0c\u6700\u7ec8\u7684\u5bf9\u5e94\u7684\u8868\u540d\u5fc5\u987b\u662f\u54ea\u4e2a\uff1f\u7b54\u6848\u662f students\uff0c\u8fd9\u8bf4\u660e\u4f18\u5148\u7ea7\uff1a@Table &gt; @Entity\u3002<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=3660\">\u8be6\u89e3 @Entity \u548c @Table \u6ce8\u89e3\u7684\u7528\u6cd5<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u770b\u4e86\u5f88\u591a\u6587\u7ae0\u90fd\u8bf4\uff0c@Entity \u548c @Table \u6ce8\u89e3\u662f Hibernate \u4e2d\u7684\u6ce8\u89e3\uff0c\u751a\u81f3\u4e00\u4e9b\u6587\u7ae0\u7684\u6807 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[1862,1863,845,1770,1864],"class_list":["post-3660","post","type-post","status-publish","format-standard","hentry","category-java","tag-entity","tag-table","tag-hibernate","tag-jpa","tag-orm"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3660","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3660"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3660\/revisions"}],"predecessor-version":[{"id":3661,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3660\/revisions\/3661"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3660"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3660"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3660"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}