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

📄 example-parentchild.html

📁 是一个中文的Hibernate库文档
💻 HTML
📖 第 1 页 / 共 2 页
字号:
    </p><pre class="programlisting">Parent p = (Parent) session.load(Parent.class, pid);Child c = new Child();p.addChild(c);session.save(c);session.flush();</pre></div><div class="sect1" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="example-parentchild-cascades"></a>22.3.&nbsp;&#32423;&#32852;&#29983;&#21629;&#21608;&#26399;&#65288;Cascading lifecycle&#65289;</h2></div></div><div></div></div><p>	&#38656;&#35201;&#26174;&#24335;&#35843;&#29992;<tt class="literal">save()</tt>&#20173;&#28982;&#24456;&#40635;&#28902;&#65292;&#25105;&#20204;&#21487;&#20197;&#29992;&#32423;&#32852;&#26469;&#35299;&#20915;&#36825;&#20010;&#38382;&#39064;&#12290;     </p><pre class="programlisting">&lt;set name="children" inverse="true" cascade="all"&gt;    &lt;key column="parent_id"/&gt;    &lt;one-to-many class="Child"/&gt;&lt;/set&gt;</pre><p>         &#36825;&#26679;&#19978;&#38754;&#30340;&#20195;&#30721;&#21487;&#20197;&#31616;&#21270;&#20026;&#65306;     </p><pre class="programlisting">Parent p = (Parent) session.load(Parent.class, pid);Child c = new Child();p.addChild(c);session.flush();</pre><p>         &#21516;&#26679;&#30340;&#65292;&#20445;&#23384;&#25110;&#21024;&#38500;<tt class="literal">Parent</tt>&#23545;&#35937;&#30340;&#26102;&#20505;&#24182;&#19981;&#38656;&#35201;&#36941;&#21382;&#20854;&#23376;&#23545;&#35937;&#12290;         &#19979;&#38754;&#30340;&#20195;&#30721;&#20250;&#21024;&#38500;&#23545;&#35937;<tt class="literal">p</tt>&#21450;&#20854;&#25152;&#26377;&#23376;&#23545;&#35937;&#23545;&#24212;&#30340;&#25968;&#25454;&#24211;&#35760;&#24405;&#12290;     </p><pre class="programlisting">Parent p = (Parent) session.load(Parent.class, pid);session.delete(p);session.flush();</pre><p>         &#28982;&#32780;&#65292;&#36825;&#27573;&#20195;&#30721;     </p><pre class="programlisting">Parent p = (Parent) session.load(Parent.class, pid);Child c = (Child) p.getChildren().iterator().next();p.getChildren().remove(c);c.setParent(null);session.flush();</pre><p>         &#19981;&#20250;&#20174;&#25968;&#25454;&#24211;&#21024;&#38500;<tt class="literal">c</tt>&#65307;&#23427;&#21482;&#20250;&#21024;&#38500;&#19982;<tt class="literal">p</tt>&#20043;&#38388;&#30340;&#36830;&#25509;&#65288;&#24182;&#19988;&#20250;&#23548;&#33268;&#36829;&#21453;<tt class="literal">NOT NULL</tt>&#32422;&#26463;&#65292;&#22312;&#36825;&#20010;&#20363;&#23376;&#20013;&#65289;&#12290;&#20320;&#38656;&#35201;&#26174;&#24335;&#35843;&#29992;<tt class="literal">delete()</tt>&#26469;&#21024;&#38500;<tt class="literal">Child</tt>&#12290;      </p><pre class="programlisting">Parent p = (Parent) session.load(Parent.class, pid);Child c = (Child) p.getChildren().iterator().next();p.getChildren().remove(c);session.delete(c);session.flush();</pre><p>         &#22312;&#25105;&#20204;&#30340;&#20363;&#23376;&#20013;&#65292;&#22914;&#26524;&#27809;&#26377;&#29238;&#23545;&#35937;&#65292;&#23376;&#23545;&#35937;&#23601;&#19981;&#24212;&#35813;&#23384;&#22312;&#65292;&#22914;&#26524;&#23558;&#23376;&#23545;&#35937;&#20174;collection&#20013;&#31227;&#38500;&#65292;&#23454;&#38469;&#19978;&#25105;&#20204;&#26159;&#24819;&#21024;&#38500;&#23427;&#12290;&#35201;&#23454;&#29616;&#36825;&#31181;&#35201;&#27714;&#65292;&#23601;&#24517;&#39035;&#20351;&#29992;<tt class="literal">cascade="all-delete-orphan"</tt>&#12290;     </p><pre class="programlisting">&lt;set name="children" inverse="true" cascade="all-delete-orphan"&gt;    &lt;key column="parent_id"/&gt;    &lt;one-to-many class="Child"/&gt;&lt;/set&gt;</pre><p>         &#27880;&#24847;&#65306;&#21363;&#20351;&#22312;collection&#19968;&#26041;&#30340;&#26144;&#23556;&#20013;&#25351;&#23450;<tt class="literal">inverse="true"</tt>&#65292;&#32423;&#32852;&#20173;&#28982;&#26159;&#36890;&#36807;&#36941;&#21382;collection&#20013;&#30340;&#20803;&#32032;&#26469;&#22788;&#29702;&#30340;&#12290;&#22914;&#26524;&#20320;&#24819;&#35201;&#36890;&#36807;&#32423;&#32852;&#36827;&#34892;&#23376;&#23545;&#35937;&#30340;&#25554;&#20837;&#12289;&#21024;&#38500;&#12289;&#26356;&#26032;&#25805;&#20316;&#65292;&#23601;&#24517;&#39035;&#25226;&#23427;&#21152;&#21040;collection&#20013;&#65292;&#21482;&#35843;&#29992;<tt class="literal">setParent()</tt>&#26159;&#19981;&#22815;&#30340;&#12290;     </p></div><div class="sect1" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="example-parentchild-update"></a>22.4.&nbsp;&#32423;&#32852;&#19982;<tt class="literal">&#26410;&#20445;&#23384;&#20540;</tt>&#65288;Cascades and <tt class="literal">unsaved-value</tt>&#65289;</h2></div></div><div></div></div><p>         &#20551;&#35774;&#25105;&#20204;&#20174;<tt class="literal">Session</tt>&#20013;&#35013;&#20837;&#20102;&#19968;&#20010;<tt class="literal">Parent</tt>&#23545;&#35937;&#65292;&#29992;&#25143;&#30028;&#38754;&#23545;&#20854;&#36827;&#34892;&#20102;&#20462;&#25913;&#65292;&#28982;&#21518;&#24076;&#26395;&#22312;&#19968;&#20010;&#26032;&#30340;Session&#37324;&#38754;&#35843;&#29992;<tt class="literal">update()</tt>&#26469;&#20445;&#23384;&#36825;&#20123;&#20462;&#25913;&#12290;&#23545;&#35937;<tt class="literal">Parent</tt>&#21253;&#21547;&#20102;&#23376;&#23545;&#35937;&#30340;&#38598;&#21512;&#65292;&#30001;&#20110;&#25171;&#24320;&#20102;&#32423;&#32852;&#26356;&#26032;&#65292;Hibernate&#38656;&#35201;&#30693;&#36947;&#21738;&#20123;Child&#23545;&#35937;&#26159;&#26032;&#23454;&#20363;&#21270;&#30340;&#65292;&#21738;&#20123;&#20195;&#34920;&#25968;&#25454;&#24211;&#20013;&#24050;&#32463;&#23384;&#22312;&#30340;&#35760;&#24405;&#12290;&#25105;&#20204;&#20551;&#35774;<tt class="literal">Parent</tt>&#21644;<tt class="literal">Child</tt>&#23545;&#35937;&#30340;&#26631;&#35782;&#23646;&#24615;&#37117;&#26159;&#33258;&#21160;&#29983;&#25104;&#30340;&#65292;&#31867;&#22411;&#20026;<tt class="literal">java.lang.Long</tt>&#12290;Hibernate&#20250;&#20351;&#29992;&#26631;&#35782;&#23646;&#24615;&#30340;&#20540;&#65292;&#21644;version &#25110; timestamp &#23646;&#24615;&#65292;&#26469;&#21028;&#26029;&#21738;&#20123;&#23376;&#23545;&#35937;&#26159;&#26032;&#30340;&#12290;(&#21442;&#35265;<a href="objectstate.html#objectstate-saveorupdate" title="11.7.&nbsp;&#33258;&#21160;&#29366;&#24577;&#26816;&#27979;">&#31532;&nbsp;11.7&nbsp;&#33410; &#8220;&#33258;&#21160;&#29366;&#24577;&#26816;&#27979;&#8221;</a>.) <span class="emphasis"><em>&#22312; Hibernate3 &#20013;,&#26174;&#24335;&#25351;&#23450;<tt class="literal">unsaved-value</tt>&#19981;&#20877;&#26159;&#24517;&#39035;&#30340;&#20102;&#12290;</em></span>     </p><p>         &#19979;&#38754;&#30340;&#20195;&#30721;&#20250;&#26356;&#26032;<tt class="literal">parent</tt>&#21644;<tt class="literal">child</tt>&#23545;&#35937;&#65292;&#24182;&#19988;&#25554;&#20837;<tt class="literal">newChild</tt>&#23545;&#35937;&#12290;     </p><pre class="programlisting">//parent and child were both loaded in a previous sessionparent.addChild(child);Child newChild = new Child();parent.addChild(newChild);session.update(parent);session.flush();</pre><p>             Well, that's all very well for the case of a generated identifier, but what about assigned identifiers             and composite identifiers? This is more difficult, since Hibernate can't use the identifier property to             distinguish between a newly instantiated object (with an identifier assigned by the user) and an              object loaded in a previous session. In this case, Hibernate will either use the timestamp or version              property, or will actually query the second-level cache or, worst case, the database, to see if the              row exists.         </p><p>         &#36825;&#23545;&#20110;&#33258;&#21160;&#29983;&#25104;&#26631;&#35782;&#30340;&#24773;&#20917;&#26159;&#38750;&#24120;&#22909;&#30340;&#65292;&#20294;&#26159;&#33258;&#20998;&#37197;&#30340;&#26631;&#35782;&#21644;&#22797;&#21512;&#26631;&#35782;&#24590;&#20040;&#21150;&#21602;&#65311;&#36825;&#26159;&#26377;&#28857;&#40635;&#28902;&#65292;&#22240;&#20026;Hibernate&#27809;&#26377;&#21150;&#27861;&#21306;&#20998;&#26032;&#23454;&#20363;&#21270;&#30340;&#23545;&#35937;&#65288;&#26631;&#35782;&#34987;&#29992;&#25143;&#25351;&#23450;&#20102;&#65289;&#21644;&#21069;&#19968;&#20010;Session&#35013;&#20837;&#30340;&#23545;&#35937;&#12290;&#22312;&#36825;&#31181;&#24773;&#20917;&#19979;&#65292;Hibernate&#20250;&#20351;&#29992;timestamp&#25110;version&#23646;&#24615;&#65292;&#25110;&#32773;&#26597;&#35810;&#31532;&#20108;&#32423;&#32531;&#23384;&#65292;&#25110;&#32773;&#26368;&#22351;&#30340;&#24773;&#20917;&#65292;&#26597;&#35810;&#25968;&#25454;&#24211;&#65292;&#26469;&#30830;&#35748;&#26159;&#21542;&#27492;&#34892;&#23384;&#22312;&#12290;</p></div><div class="sect1" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="example-parentchild-conclusion"></a>22.5.&nbsp;&#32467;&#35770;</h2></div></div><div></div></div><p>         &#36825;&#37324;&#26377;&#19981;&#23569;&#19996;&#35199;&#38656;&#35201;&#34701;&#20250;&#36143;&#36890;&#65292;&#21487;&#33021;&#20250;&#35753;&#26032;&#25163;&#24863;&#21040;&#36855;&#24785;&#12290;&#20294;&#26159;&#22312;&#23454;&#36341;&#20013;&#23427;&#20204;&#37117;&#24037;&#20316;&#22320;&#38750;&#24120;&#22909;&#12290;&#22823;&#37096;&#20998;Hibernate&#24212;&#29992;&#31243;&#24207;&#37117;&#20250;&#32463;&#24120;&#29992;&#21040;&#29238;&#23376;&#23545;&#35937;&#27169;&#24335;&#12290;     </p><p>         &#22312;&#31532;&#19968;&#27573;&#20013;&#25105;&#20204;&#26366;&#32463;&#25552;&#21040;&#21478;&#19968;&#20010;&#26041;&#26696;&#12290;&#19978;&#38754;&#30340;&#36825;&#20123;&#38382;&#39064;&#37117;&#19981;&#20250;&#20986;&#29616;&#22312;<tt class="literal">&lt;composite-element&gt;</tt>&#26144;&#23556;&#20013;&#65292;&#23427;&#20934;&#30830;&#22320;&#34920;&#36798;&#20102;&#29238;&#23376;&#20851;&#31995;&#30340;&#35821;&#20041;&#12290;&#24456;&#19981;&#24184;&#22797;&#21512;&#20803;&#32032;&#36824;&#26377;&#20004;&#20010;&#37325;&#22823;&#38480;&#21046;:&#22797;&#21512;&#20803;&#32032;&#19981;&#33021;&#25317;&#26377;collections&#65292;&#24182;&#19988;&#65292;&#38500;&#20102;&#29992;&#20110;&#24799;&#19968;&#30340;&#29238;&#23545;&#35937;&#22806;&#65292;&#23427;&#20204;&#19981;&#33021;&#20877;&#20316;&#20026;&#20854;&#23427;&#20219;&#20309;&#23454;&#20307;&#30340;&#23376;&#23545;&#35937;&#12290;     </p></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="toolsetguide.html">&#19978;&#19968;&#39029;</a>&nbsp;</td><td width="20%" align="center"><a accesskey="u" href="index.html">&#19978;&#19968;&#32423;</a></td><td width="40%" align="right">&nbsp;<a accesskey="n" href="example-weblog.html">&#19979;&#19968;&#39029;</a></td></tr><tr><td width="40%" align="left" valign="top">&#31532;&nbsp;21&nbsp;&#31456;&nbsp;&#24037;&#20855;&#31665;&#25351;&#21335;&nbsp;</td><td width="20%" align="center"><a accesskey="h" href="index.html">&#36215;&#22987;&#39029;</a></td><td width="40%" align="right" valign="top">&nbsp;&#31532;&nbsp;23&nbsp;&#31456;&nbsp;&#31034;&#20363;&#65306;Weblog &#24212;&#29992;&#31243;&#24207;</td></tr></table></div></body></html>

⌨️ 快捷键说明

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