{"id":1721,"date":"2010-06-06T10:19:08","date_gmt":"2010-06-06T02:19:08","guid":{"rendered":"\/?p=1721"},"modified":"2010-06-06T10:19:08","modified_gmt":"2010-06-06T02:19:08","slug":"1721","status":"publish","type":"post","link":"https:\/\/www.justzz.com\/spec\/1721.html","title":{"rendered":"Hibernate\u4e32\u8bb2\u7b14\u8bb0\u4e8c"},"content":{"rendered":"<p><strong>\u4e09\u3001Hibernate\u4e2d\u81ea\u5e26ID\u7684generator\u7684\u542b\u4e49\uff1a<\/strong><\/p>\n<p>1\u3001identity\uff1a\u7528\u4e8eMySql\u6570\u636e\u5e93\u3002\u7279\u70b9\uff1a\u9012\u589e<\/p>\n<p><strong>&lt;<\/strong>id name=\"id\" column=\"id\"&gt;<br \/>\n&lt;generator class=\"identity\"\/&gt;<br \/>\n&lt;\/id&gt;<\/p>\n<p>\u6ce8\uff1a\u5bf9\u4e8eMySql\u6570\u636e\u5e93\u4f7f\u7528\u9012\u589e\u5e8f\u5217\u65f6\u9700\u8981\u5728\u5efa\u8868\u65f6\u5bf9\u4e3b\u952e\u6307\u5b9a\u4e3aauto_increment\u5c5e\u6027\u3002<\/p>\n<p>2\u3001sequence\uff1a\u7528\u4e8eOracle\u6570\u636e\u5e93<\/p>\n<p><strong>&lt;<\/strong>id name=\"id\" column=\"id\"&gt;<br \/>\n&lt;generator class=\"sequence\"&gt;<br \/>\n&lt;param name=\"sequence\"&gt;\u5e8f\u5217\u540d&lt;\/param&gt;<br \/>\n&lt;\/generator&gt;<br \/>\n&lt;\/id&gt;<\/p>\n<p>3\u3001native\uff1a\u8de8\u6570\u636e\u5e93\u65f6\u4f7f\u7528\uff0c\u7531\u5e95\u5c42\u65b9\u8a00\u4ea7\u751f\u3002\u9047Oracle\u91c7\u7528sequence; \u9047MySQL\u91c7\u7528identity\u3002Default.sequence\u4e3ahibernate_sequence<\/p>\n<p><strong>&lt;<\/strong>id name=\"id\" column=\"id\"&gt;<br \/>\n&lt;generator class=\"native\"\/&gt;<br \/>\n&lt;\/id&gt;<\/p>\n<p>\u6ce8\uff1a\u4f7f\u7528native\u65f6Hibernate\u9ed8\u8ba4\u4f1a\u53bb\u67e5\u627eOracle\u4e2d\u7684hibernate_sequence\u5e8f\u5217\u3002\u5982\u679cOracle\u4e2d\u6ca1\u6709\u8be5\u5e8f\u5217\uff0c\u8fdeOracle\u6570\u636e\u5e93\u65f6\u4f1a\u62a5\u9519\u3002<\/p>\n<p>4\u3001hilo\uff1a\u901a\u8fc7\u9ad8\u4f4e\u4f4d\u5408\u6210id\uff0c\u5148\u5efa\u8868hi_value\uff0c\u518d\u5efa\u5217next_value\u3002\u5fc5\u987b\u8981\u6709\u521d\u59cb\u503c\u3002<\/p>\n<p>&lt;id name=\"id\" column=\"id\"&gt;<br \/>\n&lt;generator class=\"hilo\"&gt;<br \/>\n&lt;param name=\"table\"&gt;high_val&lt;\/param&gt;<br \/>\n&lt;param name=\"column\"&gt;nextval&lt;\/param&gt;<br \/>\n&lt;param name=\"max_lo\"&gt;5&lt;\/param&gt;<br \/>\n&lt;\/generator&gt;<br \/>\n&lt;\/id&gt;<\/p>\n<p>5\u3001sequencehilo\uff1a\u540c\u8fc7\u9ad8\u4f4e\u4f4d\u5408\u6210id\uff0c\u5efa\u4e00\u4e2asequence\u5e8f\u5217\uff0c\u4e0d\u7528\u5efa\u8868\u3002<\/p>\n<p>&lt;id name=\"id\" column=\"id\"&gt;<br \/>\n&lt;generator class=\"hilo\"&gt;<br \/>\n&lt;param name=\"sequence\"&gt;high_val_seq&lt;\/param&gt;<br \/>\n&lt;param name=\"max_lo\"&gt;5&lt;\/param&gt;<br \/>\n&lt;\/generator&gt;<br \/>\n&lt;\/id&gt;<br \/>\n6\u3001assigned\uff1a\u7528\u6237\u81ea\u5b9a\u4e49id;<br \/>\n&lt;id name=\"id\" column=\"id\"&gt;<br \/>\n&lt;generator class=\"assigned\"\/&gt;<br \/>\n&lt;\/id&gt;<\/p>\n<p>7\u3001foreign\uff1a\u7528\u4e8e\u4e00\u5bf9\u4e00\u5173\u7cfb\u5171\u4eab\u4e3b\u5065\u65f6\uff0c\u4e24id\u503c\u4e00\u6837\u3002<\/p>\n<p><strong>\u56db\u3001Hibernate\u7c7b\u5173\u7cfb\u7684\u5904\u7406\uff1a<\/strong><\/p>\n<p>1\u3001One\u2015to\u2015One\u5173\u7cfb\u5728\u6570\u636e\u5e93\u4e2d\u5982\u4f55\u4f53\u73b0\uff0c\u5728JavaBean\u4e2d\u5982\u4f55\u4f53\u73b0\uff0c\u5728Hibernate\u4e2d\u5982\u4f55\u6620\u5c04one\uff0dto\uff0done\u5173\u7cfb\u3002<\/p>\n<ul>\n<li>1\u3001\u6570\u636e\u5e93\u4e2d\uff1a\u4e00\u4e2a\u8868\u7684\u5916\u5065\u5bf9\u5e94\u53e6\u4e00\u4e2a\u8868\u7684\u4e3b\u5065\uff0c\u5916\u5065\u8981\u52a0\u4e0aUnique\u7ea6\u675f\u3002\u6216\u8005\u662f\u4e24\u4e2a\u8868\u5171\u4eab\u4e00\u4e2a\u4e3b\u5065\u3002<br \/>\n2\u3001javaBean\u4e2d\uff1a\u5728JavaBean\u4e2d\u589e\u52a0\u4e00\u4e2a\u5c5e\u6027\uff0c\u5373\u53e6\u5916\u4e00\u4e2a\u5bf9\u8c61\u7684\u5f15\u7528\uff0c\u53ef\u4ee5\u5355\u5411\u4e5f\u53ef\u4ee5\u53cc\u5411\u3002<br \/>\n3\u3001\u5728hibernate\u4e2d\uff1a<\/li>\n<\/ul>\n<p>\u4e3b\u5065\u6620\u5c04\uff1a\u90fd\u662fone\uff0dto\uff0done \u8981\u7528foreign\u751f\u6210\u7b56\u7565\u3002<\/p>\n<p>\u5916\u5065\u6620\u5c04\uff1a\u4e3b\u8868\u4e2d\u7528one\uff0dto\uff0done\uff0c\u526f\u8868\u901a\u8fc7property\uff0dref\u00a0 many\uff0dto\uff0done<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7699\" src=\"https:\/\/www.justzz.com\/spec\/wp-content\/uploads\/sites\/3\/2019\/10\/201066-1017315.jpg\" width=\"652\" height=\"431\" alt=\"201066-1017315\" srcset=\"https:\/\/www.justzz.com\/spec\/files\/2019\/10\/201066-1017315.jpg 652w, https:\/\/www.justzz.com\/spec\/files\/2019\/10\/201066-1017315-300x198.jpg 300w\" sizes=\"auto, (max-width: 652px) 100vw, 652px\" \/><\/p>\n<p>\u5efa\u8868\u8bed\u53e5\u5982\u4e0b\uff1a<\/p>\n<p><strong>c<\/strong>reate table engine_fk (<br \/>\nid number(10,0) not null,<br \/>\nmodel varchar2(20) not null,<br \/>\nmanufacturer varchar2(50) not null,<br \/>\nproducedate date,<br \/>\ncarid number(10,0) unique,<br \/>\nprimary key (id)<br \/>\n);<br \/>\nalter table engine_fk<br \/>\nadd constraint fk_engine_car_fk<br \/>\nforeign key (carid)<br \/>\nreferences car_fk(id);<\/p>\n<p>2\u3001Many\uff0dto\uff0dOne\u5173\u7cfb\u5728\u6570\u636e\u5e93\u4e2d\u5982\u4f55\u4f53\u73b0\uff0c\u5728JavaBean\u4e2d\u5982\u4f55\u4f53\u73b0\uff0c\u5728Hibernate\u4e2d\u5982\u4f55\u6620\u5c04many\uff0dto\uff0don\u5173\u7cfb\u3002<\/p>\n<ul>\n<li>1\u3001\u6570\u636e\u5e93\u4e2d\uff1a\u4e00\u4e2a\u8868\u7684\u4e3b\u5065\u5bf9\u5e94\u53e6\u4e00\u4e2a\u8868\u7684\u5916\u5065\u3002<br \/>\n2\u3001javaBean\u4e2d\uff1a\u4e00\u4e2a\u5bf9\u8c61\u5305\u542b\u53e6\u4e00\u4e2a\u5bf9\u8c61\u7684\u4e00\u4e2a\u5f15\u7528\uff0c\u53e6\u4e00\u4e2a\u5bf9\u8c61\u5305\u542b\u8fd9\u4e2a\u5bf9\u8c61\u7684\u4e00\u7ec4\uff08\u96c6\u5408\uff0c\u7528set\uff09\u5f15\u7528\u3002<br \/>\n3\u3001\u5728hibernate\u4e2d\uff1a\u7528set.key\u6307\u5b9a\u5177\u4f53\u5217\u3002<\/li>\n<\/ul>\n<p>\u5728Set\u4e00\u7aef\u4e00\u5b9a\u8981\u5199inverse\uff1d\u201ctrue\u201d\u8868\u793a\u5ffd\u7565\u96c6\u5408\u4e00\u7aef\u7684\u53d8\u5316\u3002<\/p>\n<p>\u4ee5Order\u7c7b\u548cItem\u7c7b \u7684\u4e00\u5bf9\u591a\u5173\u7cfb\u4e3a\u4f8b\u5b50\uff08\u8be6\u89c1\u4ee3\u7801\uff09\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7700\" src=\"https:\/\/www.justzz.com\/spec\/wp-content\/uploads\/sites\/3\/2019\/10\/201066-101723812.jpg\" width=\"421\" height=\"208\" alt=\"201066-101723812\" srcset=\"https:\/\/www.justzz.com\/spec\/files\/2019\/10\/201066-101723812.jpg 421w, https:\/\/www.justzz.com\/spec\/files\/2019\/10\/201066-101723812-300x148.jpg 300w\" sizes=\"auto, (max-width: 421px) 100vw, 421px\" \/><\/p>\n<p><strong>\u5c0f\u7ed3\uff1a<\/strong><\/p>\n<p>\u5bf9\u8c61\u7684\u5173\u8054\u5173\u7cfb\u6709\u4e00\u5bf9\u4e00\u3001\u4e00\u5bf9\u591a\u3001\u591a\u5bf9\u4e00\u548c\u591a\u5bf9\u591a\uff0c\u5176\u4e2d\u591a\u5bf9\u4e00\u548c\u4e00\u5bf9\u591a\u5173\u7cfb\u6bd4\u8f83\u5e38\u89c1\u3002<br \/>\n\u4e00\u5bf9\u4e00\u5173\u7cfb\u6709\u4e24\u4e2d\u65b9\u5f0f\uff1a\u4ee5\u4e3b\u952e\u5173\u8054\u548c\u4ee5\u5916\u952e\u5173\u8054\u3002<\/p>\n<p>\u4e00\u5bf9\u591a\u548c\u591a\u5bf9\u4e00\u901a\u5e38\u5e94\u8bbe\u7f6e\u4e3a\u53cc\u5411\u5173\u8054\uff0c\u5e76\u5728\u201c\u4e00\u201d\u8fd9\u4e00\u65b9\u8bbe\u7f6einverse=\"true\"\uff08\u6bd4\u5982\u4e00\u4e2a\u73ed\u7ea7\u5bf9\u5e94\u591a\u4e2a\u5b66\u751f\uff0c\u5728\u73ed\u7ea7\u8fd9\u4e00\u8fb9\u8bbe\u7f6einverse=\"true\"\uff09\u3002\u8bbe\u7f6e\u4e86inverse=\"true\"\u8fd9\u4e00\u7aef\u6bd4\u8f83\u6e05\u95f2\uff0c\u5b83\u628a\u63a7\u5236\u6743\u4ea4\u7ed9\u4e86\u53e6\u4e00\u7aef\u3002\u8ba9\u201c\u591a\u201d\u8fd9\u4e00\u7aef\u6709\u63a7\u5236\u6743\u3002<\/p>\n<p>\u591a\u5bf9\u591a \u53ef\u4ee5\u501f\u52a9\u4e2d\u95f4\u8868\u6765\u4fdd\u5b58\u4e24\u4e2a\u5b9e\u4f53\u4e4b\u95f4\u7684\u5173\u7cfb\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e09\u3001Hibernate\u4e2d\u81ea\u5e26ID\u7684generator\u7684\u542b\u4e49\uff1a 1\u3001identity\uff1a\u7528\u4e8eMySql\u6570\u636e\u5e93\u3002\u7279\u70b9\uff1a\u9012\u589e &lt;id name=\"id\" column=\"id\"&gt; &lt;generator class=\"identity\"\/&#038;g&hellip;<\/p>\n","protected":false},"author":13,"featured_media":7700,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-1721","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-t"],"_links":{"self":[{"href":"https:\/\/www.justzz.com\/spec\/wp-json\/wp\/v2\/posts\/1721","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.justzz.com\/spec\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.justzz.com\/spec\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.justzz.com\/spec\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.justzz.com\/spec\/wp-json\/wp\/v2\/comments?post=1721"}],"version-history":[{"count":0,"href":"https:\/\/www.justzz.com\/spec\/wp-json\/wp\/v2\/posts\/1721\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.justzz.com\/spec\/wp-json\/wp\/v2\/media\/7700"}],"wp:attachment":[{"href":"https:\/\/www.justzz.com\/spec\/wp-json\/wp\/v2\/media?parent=1721"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.justzz.com\/spec\/wp-json\/wp\/v2\/categories?post=1721"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.justzz.com\/spec\/wp-json\/wp\/v2\/tags?post=1721"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}