⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 example-parentchild.html

📁 是一个中文的Hibernate库文档
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<html><head>      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">   <title>&#31532;&nbsp;22&nbsp;&#31456;&nbsp;&#31034;&#20363;&#65306;&#29238;&#23376;&#20851;&#31995;(Parent Child Relationships)</title><link rel="stylesheet" href="../shared/css/html.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets V1.65.1"><link rel="home" href="index.html" title="HIBERNATE - &#31526;&#21512;Java&#20064;&#24815;&#30340;&#20851;&#31995;&#25968;&#25454;&#24211;&#25345;&#20037;&#21270;"><link rel="up" href="index.html" title="HIBERNATE - &#31526;&#21512;Java&#20064;&#24815;&#30340;&#20851;&#31995;&#25968;&#25454;&#24211;&#25345;&#20037;&#21270;"><link rel="previous" href="toolsetguide.html" title="&#31532;&nbsp;21&nbsp;&#31456;&nbsp;&#24037;&#20855;&#31665;&#25351;&#21335;"><link rel="next" href="example-weblog.html" title="&#31532;&nbsp;23&nbsp;&#31456;&nbsp;&#31034;&#20363;&#65306;Weblog &#24212;&#29992;&#31243;&#24207;"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">&#31532;&nbsp;22&nbsp;&#31456;&nbsp;&#31034;&#20363;&#65306;&#29238;&#23376;&#20851;&#31995;(Parent Child Relationships)</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="toolsetguide.html">&#19978;&#19968;&#39029;</a>&nbsp;</td><th width="60%" align="center">&nbsp;</th><td width="20%" align="right">&nbsp;<a accesskey="n" href="example-weblog.html">&#19979;&#19968;&#39029;</a></td></tr></table><hr></div><div class="chapter" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title"><a name="example-parentchild"></a>&#31532;&nbsp;22&nbsp;&#31456;&nbsp;&#31034;&#20363;&#65306;&#29238;&#23376;&#20851;&#31995;(Parent Child Relationships)</h2></div></div><div></div></div><p>    &#21018;&#21018;&#25509;&#35302;Hibernate&#30340;&#20154;&#22823;&#22810;&#26159;&#20174;&#29238;&#23376;&#20851;&#31995;&#65288;parent / child type relationship&#65289;&#30340;&#24314;&#27169;&#20837;&#25163;&#30340;&#12290;&#29238;&#23376;&#20851;&#31995;&#30340;&#24314;&#27169;&#26377;&#20004;&#31181;&#26041;&#27861;&#12290;&#30001;&#20110;&#31181;&#31181;&#21407;&#22240;&#65292;&#26368;&#26041;&#20415;&#30340;&#26041;&#27861;&#26159;&#25226;<tt class="literal">Parent</tt>&#21644;<tt class="literal">Child</tt>&#37117;&#24314;&#27169;&#25104;&#23454;&#20307;&#31867;&#65292;&#24182;&#21019;&#24314;&#19968;&#20010;&#20174;<tt class="literal">Parent</tt>&#25351;&#21521;<tt class="literal">Child</tt>&#30340;&lt;one-to-many&gt;&#20851;&#32852;&#65292;&#23545;&#26032;&#25163;&#26469;&#35828;&#23588;&#20854;&#22914;&#27492;&#12290;&#36824;&#26377;&#19968;&#31181;&#26041;&#27861;&#65292;&#23601;&#26159;&#23558;<tt class="literal">Child</tt>&#22768;&#26126;&#20026;&#19968;&#20010;<tt class="literal">&lt;composite-element&gt;</tt>&#65288;&#32452;&#21512;&#20803;&#32032;&#65289;&#12290; &#20107;&#23454;&#19978;&#22312;Hibernate&#20013;one to many&#20851;&#32852;&#30340;&#40664;&#35748;&#35821;&#20041;&#36828;&#27809;&#26377;composite element&#36148;&#36817;parent / child&#20851;&#31995;&#30340;&#36890;&#24120;&#35821;&#20041;&#12290;&#19979;&#38754;&#25105;&#20204;&#20250;&#38416;&#36848;&#22914;&#20309;&#20351;&#29992;<span class="emphasis"><em>&#24102;&#26377;&#32423;&#32852;&#30340;&#21452;&#21521;&#19968;&#23545;&#22810;&#20851;&#32852;(bidirectional one to many association with cascades)</em></span>&#21435;&#24314;&#31435;&#26377;&#25928;&#12289;&#20248;&#32654;&#30340;parent / child&#20851;&#31995;&#12290;&#36825;&#19968;&#28857;&#20063;&#19981;&#38590;&#65281;    </p><div class="sect1" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="example-parentchild-collections"></a>22.1.&nbsp;&#20851;&#20110;collections&#38656;&#35201;&#27880;&#24847;&#30340;&#19968;&#28857;</h2></div></div><div></div></div><p>            Hibernate collections&#34987;&#24403;&#20316;&#20854;&#25152;&#23646;&#23454;&#20307;&#32780;&#19981;&#26159;&#20854;&#21253;&#21547;&#23454;&#20307;&#30340;&#19968;&#20010;&#36923;&#36753;&#37096;&#20998;&#12290;&#36825;&#38750;&#24120;&#37325;&#35201;&#65281;&#23427;&#20027;&#35201;&#20307;&#29616;&#20026;&#20197;&#19979;&#20960;&#28857;&#65306;    </p><div class="itemizedlist"><ul type="disc"><li><p>            &#24403;&#21024;&#38500;&#25110;&#22686;&#21152;collection&#20013;&#23545;&#35937;&#30340;&#26102;&#20505;&#65292;collection&#25152;&#23646;&#32773;&#30340;&#29256;&#26412;&#20540;&#20250;&#36882;&#22686;&#12290;        </p></li><li><p>	    &#22914;&#26524;&#19968;&#20010;&#20174;collection&#20013;&#31227;&#38500;&#30340;&#23545;&#35937;&#26159;&#19968;&#20010;&#20540;&#31867;&#22411;(value type)&#30340;&#23454;&#20363;&#65292;&#27604;&#22914;composite element&#65292;&#37027;&#20040;&#36825;&#20010;&#23545;&#35937;&#30340;&#25345;&#20037;&#21270;&#29366;&#24577;&#23558;&#20250;&#32456;&#27490;&#65292;&#20854;&#22312;&#25968;&#25454;&#24211;&#20013;&#23545;&#24212;&#30340;&#35760;&#24405;&#20250;&#34987;&#21024;&#38500;&#12290;&#21516;&#26679;&#30340;&#65292;&#21521;collection&#22686;&#21152;&#19968;&#20010;value type&#30340;&#23454;&#20363;&#23558;&#20250;&#20351;&#20043;&#31435;&#21363;&#34987;&#25345;&#20037;&#21270;&#12290;        </p></li><li><p>            &#21478;&#19968;&#26041;&#38754;&#65292;&#22914;&#26524;&#20174;&#19968;&#23545;&#22810;&#25110;&#22810;&#23545;&#22810;&#20851;&#32852;&#30340;collection&#20013;&#31227;&#38500;&#19968;&#20010;&#23454;&#20307;&#65292;&#22312;&#32570;&#30465;&#24773;&#20917;&#19979;&#36825;&#20010;&#23545;&#35937;&#24182;&#19981;&#20250;&#34987;&#21024;&#38500;&#12290;&#36825;&#20010;&#34892;&#20026;&#26159;&#23436;&#20840;&#21512;&#20046;&#36923;&#36753;&#30340;&#65293;&#65293;&#25913;&#21464;&#19968;&#20010;&#23454;&#20307;&#30340;&#20869;&#37096;&#29366;&#24577;&#19981;&#24212;&#35813;&#20351;&#19982;&#23427;&#20851;&#32852;&#30340;&#23454;&#20307;&#28040;&#22833;&#25481;&#65281;&#21516;&#26679;&#30340;&#65292;&#21521;collection&#22686;&#21152;&#19968;&#20010;&#23454;&#20307;&#19981;&#20250;&#20351;&#20043;&#34987;&#25345;&#20037;&#21270;&#12290;        </p></li></ul></div><p>        &#23454;&#38469;&#19978;&#65292;&#21521;Collection&#22686;&#21152;&#19968;&#20010;&#23454;&#20307;&#30340;&#32570;&#30465;&#21160;&#20316;&#21482;&#26159;&#22312;&#20004;&#20010;&#23454;&#20307;&#20043;&#38388;&#21019;&#24314;&#19968;&#20010;&#36830;&#25509;&#32780;&#24050;&#65292;&#21516;&#26679;&#31227;&#38500;&#30340;&#26102;&#20505;&#20063;&#21482;&#26159;&#21024;&#38500;&#36830;&#25509;&#12290;&#36825;&#31181;&#22788;&#29702;&#23545;&#20110;&#25152;&#26377;&#30340;&#24773;&#20917;&#37117;&#26159;&#21512;&#36866;&#30340;&#12290;&#23545;&#20110;&#29238;&#23376;&#20851;&#31995;&#21017;&#26159;&#23436;&#20840;&#19981;&#36866;&#21512;&#30340;&#65292;&#22312;&#36825;&#31181;&#20851;&#31995;&#19979;&#65292;&#23376;&#23545;&#35937;&#30340;&#29983;&#23384;&#32465;&#23450;&#20110;&#29238;&#23545;&#35937;&#30340;&#29983;&#23384;&#21608;&#26399;&#12290;    </p></div><div class="sect1" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="example-parentchild-bidir"></a>22.2.&nbsp;&#21452;&#21521;&#30340;&#19968;&#23545;&#22810;&#20851;&#31995;(Bidirectional one-to-many)</h2></div></div><div></div></div><p>        &#20551;&#35774;&#25105;&#20204;&#35201;&#23454;&#29616;&#19968;&#20010;&#31616;&#21333;&#30340;&#20174;Parent&#21040;Child&#30340;&lt;one-to-many&gt;&#20851;&#32852;&#12290;    </p><pre class="programlisting">&lt;set name="children"&gt;    &lt;key column="parent_id"/&gt;    &lt;one-to-many class="Child"/&gt;&lt;/set&gt;</pre><p>        &#22914;&#26524;&#25105;&#20204;&#36816;&#34892;&#19979;&#38754;&#30340;&#20195;&#30721;    </p><pre class="programlisting">Parent p = .....;Child c = new Child();p.getChildren().add(c);session.save(c);session.flush();</pre><p>        Hibernate&#20250;&#20135;&#29983;&#20004;&#26465;SQL&#35821;&#21477;&#65306;    </p><div class="itemizedlist"><ul type="disc"><li><p>&#19968;&#26465;<tt class="literal">INSERT</tt>&#35821;&#21477;&#65292;&#20026;<tt class="literal">c</tt>&#21019;&#24314;&#19968;&#26465;&#35760;&#24405;</p></li><li><p>	    &#19968;&#26465;<tt class="literal">UPDATE</tt>&#35821;&#21477;&#65292;&#21019;&#24314;&#20174;<tt class="literal">p</tt>&#21040;<tt class="literal">c</tt>&#30340;&#36830;&#25509;        </p></li></ul></div><p>        &#36825;&#26679;&#20570;&#19981;&#20165;&#25928;&#29575;&#20302;&#65292;&#32780;&#19988;&#36829;&#21453;&#20102;&#21015;<tt class="literal">parent_id</tt>&#38750;&#31354;&#30340;&#38480;&#21046;&#12290;&#25105;&#20204;&#21487;&#20197;&#36890;&#36807;&#22312;&#38598;&#21512;&#31867;&#26144;&#23556;&#19978;&#25351;&#23450;<tt class="literal">not-null="true"</tt>&#26469;&#35299;&#20915;&#36829;&#21453;&#38750;&#31354;&#32422;&#26463;&#30340;&#38382;&#39064;&#65306;    </p><pre class="programlisting">&lt;set name="children"&gt;    &lt;key column="parent_id" not-null="true"/&gt;    &lt;one-to-many class="Child"/&gt;&lt;/set&gt;</pre><p>    	&#28982;&#32780;&#65292;&#36825;&#24182;&#38750;&#26159;&#25512;&#33616;&#30340;&#35299;&#20915;&#26041;&#27861;&#12290;   	</p><p>        &#36825;&#31181;&#29616;&#35937;&#30340;&#26681;&#26412;&#21407;&#22240;&#26159;&#20174;<tt class="literal">p</tt>&#21040;<tt class="literal">c</tt>&#30340;&#36830;&#25509;&#65288;&#22806;&#38190;parent_id&#65289;&#27809;&#26377;&#34987;&#24403;&#20316;<tt class="literal">Child</tt>&#23545;&#35937;&#29366;&#24577;&#30340;&#19968;&#37096;&#20998;&#65292;&#22240;&#32780;&#27809;&#26377;&#22312;INSERT&#35821;&#21477;&#20013;&#34987;&#21019;&#24314;&#12290;&#22240;&#27492;&#35299;&#20915;&#30340;&#21150;&#27861;&#23601;&#26159;&#25226;&#36825;&#20010;&#36830;&#25509;&#28155;&#21152;&#21040;Child&#30340;&#26144;&#23556;&#20013;&#12290;    </p><pre class="programlisting">&lt;many-to-one name="parent" column="parent_id" not-null="true"/&gt;</pre><p>        &#65288;&#25105;&#20204;&#36824;&#38656;&#35201;&#20026;&#31867;<tt class="literal">Child</tt>&#28155;&#21152;<tt class="literal">parent</tt>&#23646;&#24615;&#65289;    </p><p>        &#29616;&#22312;&#23454;&#20307;<tt class="literal">Child</tt>&#22312;&#31649;&#29702;&#36830;&#25509;&#30340;&#29366;&#24577;&#65292;&#20026;&#20102;&#20351;collection&#19981;&#26356;&#26032;&#36830;&#25509;&#65292;&#25105;&#20204;&#20351;&#29992;<tt class="literal">inverse</tt>&#23646;&#24615;&#12290;    </p><pre class="programlisting">&lt;set name="children" inverse="true"&gt;    &lt;key column="parent_id"/&gt;    &lt;one-to-many class="Child"/&gt;&lt;/set&gt;</pre><p>        &#19979;&#38754;&#30340;&#20195;&#30721;&#26159;&#29992;&#26469;&#28155;&#21152;&#19968;&#20010;&#26032;&#30340;<tt class="literal">Child</tt>    </p><pre class="programlisting">Parent p = (Parent) session.load(Parent.class, pid);Child c = new Child();c.setParent(p);p.getChildren().add(c);session.save(c);session.flush();</pre><p>        &#29616;&#22312;&#65292;&#21482;&#20250;&#26377;&#19968;&#26465;<tt class="literal">INSERT</tt>&#35821;&#21477;&#34987;&#25191;&#34892;&#65281;    </p><p>        &#20026;&#20102;&#35753;&#20107;&#24773;&#21464;&#24471;&#20117;&#20117;&#26377;&#26465;&#65292;&#21487;&#20197;&#20026;<tt class="literal">Parent</tt>&#21152;&#19968;&#20010;<tt class="literal">addChild()</tt>&#26041;&#27861;&#12290;    </p><pre class="programlisting">public void addChild(Child c) {    c.setParent(this);    children.add(c);}</pre><p>        &#29616;&#22312;&#65292;&#28155;&#21152;<tt class="literal">Child</tt>&#30340;&#20195;&#30721;&#23601;&#26159;&#36825;&#26679;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -