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

📄 objectstate.html

📁 是一个中文的Hibernate库文档
💻 HTML
📖 第 1 页 / 共 5 页
字号:
    "SELECT {cat.*} FROM CAT {cat} WHERE ROWNUM&lt;10",    "cat",    Cat.class).list();</pre><pre class="programlisting">List cats = session.createSQLQuery(    "SELECT {cat}.ID AS {cat.id}, {cat}.SEX AS {cat.sex}, " +           "{cat}.MATE AS {cat.mate}, {cat}.SUBCLASS AS {cat.class}, ... " +    "FROM CAT {cat} WHERE ROWNUM&lt;10",    "cat",    Cat.class).list()</pre><p>                &#21644;Hibernate&#26597;&#35810;&#19968;&#26679;&#65292;SQL&#26597;&#35810;&#20063;&#21487;&#20197;&#21253;&#21547;&#21629;&#21517;&#21442;&#25968;&#21644;&#21344;&#20301;&#21442;&#25968;&#12290;			  &#21487;&#20197;&#22312;<a href="querysql.html" title="&#31532;&nbsp;17&nbsp;&#31456;&nbsp;Native SQL&#26597;&#35810;">&#31532;&nbsp;17&nbsp;&#31456; <i>Native SQL&#26597;&#35810;</i></a>&#25214;&#21040;&#26356;&#22810;&#20851;&#20110;Hibernate&#20013;&#21407;&#29983;SQL(native SQL)&#30340;&#20449;&#24687;&#12290;            </p></div></div><div class="sect1" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="objectstate-modifying"></a>11.5.&nbsp;&#20462;&#25913;&#25345;&#20037;&#23545;&#35937;</h2></div></div><div></div></div><p>            <span class="emphasis"><em>&#20107;&#21153;&#20013;&#30340;&#25345;&#20037;&#23454;&#20363;</em></span>&#65288;&#23601;&#26159;&#36890;&#36807;<tt class="literal">session</tt>&#35013;&#36733;&#12289;&#20445;&#23384;&#12289;&#21019;&#24314;&#25110;&#32773;&#26597;&#35810;&#20986;&#30340;&#23545;&#35937;&#65289;            &#34987;&#24212;&#29992;&#31243;&#24207;&#25805;&#20316;&#25152;&#36896;&#25104;&#30340;&#20219;&#20309;&#20462;&#25913;&#37117;&#20250;&#22312;<tt class="literal">Session</tt>&#34987;<span class="emphasis"><em>&#21047;&#20986;&#65288;flushed&#65289;</em></span>&#30340;&#26102;&#20505;&#34987;&#25345;&#20037;&#21270;&#65288;&#26412;&#31456;&#21518;&#38754;&#20250;&#35814;&#32454;&#35752;&#35770;&#65289;&#12290;			&#36825;&#37324;&#19981;&#38656;&#35201;&#35843;&#29992;&#26576;&#20010;&#29305;&#23450;&#30340;&#26041;&#27861;&#65288;&#27604;&#22914;<tt class="literal">update()</tt>&#65292;&#35774;&#35745;&#23427;&#30340;&#30446;&#30340;&#26159;&#19981;&#21516;&#30340;&#65289;&#23558;&#20320;&#30340;&#20462;&#25913;&#25345;&#20037;&#21270;&#12290;            &#25152;&#20197;&#26368;&#30452;&#25509;&#30340;&#26356;&#26032;&#19968;&#20010;&#23545;&#35937;&#30340;&#26041;&#27861;&#23601;&#26159;&#22312;<tt class="literal">Session</tt>&#22788;&#20110;&#25171;&#24320;&#29366;&#24577;&#26102;<tt class="literal">load()</tt>&#23427;&#65292;&#28982;&#21518;&#30452;&#25509;&#20462;&#25913;&#21363;&#21487;&#65306;        </p><pre class="programlisting">DomesticCat cat = (DomesticCat) sess.load( Cat.class, new Long(69) );cat.setName("PK");sess.flush();  // changes to cat are automatically detected and persisted</pre><p>        	&#26377;&#26102;&#36825;&#31181;&#31243;&#24207;&#27169;&#22411;&#25928;&#29575;&#20302;&#19979;&#65292;&#22240;&#20026;&#23427;&#22312;&#21516;&#19968;Session&#37324;&#38656;&#35201;&#19968;&#26465;SQL <tt class="literal">SELECT</tt>&#35821;&#21477;&#65288;&#29992;&#20110;&#21152;&#36733;&#23545;&#35937;&#65289;        	&#20197;&#21450;&#19968;&#26465;SQL <tt class="literal">UPDATE</tt>&#35821;&#21477;(&#25345;&#20037;&#21270;&#26356;&#26032;&#30340;&#29366;&#24577;)&#12290;        	&#20026;&#27492;Hibernate&#25552;&#20379;&#20102;&#21478;&#19968;&#31181;&#36884;&#24452;&#65292;&#20351;&#29992;&#33073;&#31649;(detached)&#23454;&#20363;&#12290;        </p><p>            <span class="emphasis"><em>&#35831;&#27880;&#24847;Hibernate&#26412;&#36523;&#19981;&#25552;&#20379;&#30452;&#25509;&#25191;&#34892;<tt class="literal">UPDATE</tt>&#25110;<tt class="literal">DELETE</tt>&#35821;&#21477;&#30340;API&#12290;            Hibernate&#25552;&#20379;&#30340;&#26159;<span class="emphasis"><em>&#29366;&#24577;&#31649;&#29702;(state management)</em></span>&#26381;&#21153;&#65292;&#20320;&#19981;&#24517;&#32771;&#34385;&#35201;&#20351;&#29992;&#30340;<span class="emphasis"><em>&#35821;&#21477;(statements)</em></span>&#12290;			JDBC&#26159;&#20986;&#33394;&#30340;&#25191;&#34892;SQL&#35821;&#21477;&#30340;API&#65292;&#20219;&#20309;&#26102;&#20505;&#35843;&#29992;<tt class="literal">session.connection()</tt>&#20320;&#37117;&#21487;&#20197;&#24471;&#21040;&#19968;&#20010;JDBC <tt class="literal">Connection</tt>&#23545;&#35937;&#12290;			&#27492;&#22806;&#65292;&#22312;&#32852;&#26426;&#20107;&#21153;&#22788;&#29702;(OLTP)&#31243;&#24207;&#20013;&#65292;&#22823;&#37327;&#25805;&#20316;(mass operations)&#19982;&#23545;&#35937;/&#20851;&#31995;&#26144;&#23556;&#30340;&#35266;&#28857;&#26159;&#30456;&#20914;&#31361;&#30340;&#12290;            Hibernate&#30340;&#23558;&#26469;&#29256;&#26412;&#21487;&#33021;&#20250;&#25552;&#20379;&#19987;&#38376;&#30340;&#36827;&#34892;&#22823;&#37327;&#25805;&#20316;(mass operation)&#30340;&#21151;&#33021;&#12290;                        &#21442;&#32771;<a href="batch.html" title="&#31532;&nbsp;14&nbsp;&#31456;&nbsp;&#25209;&#37327;&#22788;&#29702;&#65288;Batch processing&#65289;">&#31532;&nbsp;14&nbsp;&#31456; <i>&#25209;&#37327;&#22788;&#29702;&#65288;Batch processing&#65289;</i></a>&#65292;&#23547;&#25214;&#19968;&#20123;&#21487;&#29992;&#30340;&#25209;&#37327;(batch)&#25805;&#20316;&#25216;&#24039;&#12290;</em></span>        </p></div><div class="sect1" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="objectstate-detached"></a>11.6.&nbsp;&#20462;&#25913;&#33073;&#31649;(Detached)&#23545;&#35937;</h2></div></div><div></div></div><p>            &#24456;&#22810;&#31243;&#24207;&#38656;&#35201;&#22312;&#26576;&#20010;&#20107;&#21153;&#20013;&#33719;&#21462;&#23545;&#35937;&#65292;&#28982;&#21518;&#23558;&#23545;&#35937;&#21457;&#36865;&#21040;&#30028;&#38754;&#23618;&#21435;&#25805;&#20316;&#65292;&#26368;&#21518;&#22312;&#19968;&#20010;&#26032;&#30340;&#20107;&#21153;&#20445;&#23384;&#25152;&#20570;&#30340;&#20462;&#25913;&#12290;            &#22312;&#39640;&#24182;&#21457;&#35775;&#38382;&#30340;&#29615;&#22659;&#20013;&#20351;&#29992;&#36825;&#31181;&#26041;&#24335;&#65292;&#36890;&#24120;&#20351;&#29992;&#38468;&#24102;&#29256;&#26412;&#20449;&#24687;&#30340;&#25968;&#25454;&#26469;&#20445;&#35777;&#36825;&#20123;&#8220;&#38271;&#8220;&#24037;&#20316;&#21333;&#20803;&#20043;&#38388;&#30340;&#38548;&#31163;&#12290;        </p><p>        	Hibernate&#36890;&#36807;&#25552;&#20379;&#20351;&#29992;<tt class="literal">Session.update()</tt>&#25110;<tt class="literal">Session.merge()</tt>&#26041;&#27861;        	&#37325;&#26032;&#20851;&#32852;&#33073;&#31649;&#23454;&#20363;&#30340;&#21150;&#27861;&#26469;&#25903;&#25345;&#36825;&#31181;&#27169;&#22411;&#12290;        </p><pre class="programlisting">// in the first sessionCat cat = (Cat) firstSession.load(Cat.class, catId);Cat potentialMate = new Cat();firstSession.save(potentialMate);// in a higher layer of the applicationcat.setMate(potentialMate);// later, in a new sessionsecondSession.update(cat);  // update catsecondSession.update(mate); // update mate</pre><p>            &#22914;&#26524;&#20855;&#26377;<tt class="literal">catId</tt>&#25345;&#20037;&#21270;&#26631;&#35782;&#30340;<tt class="literal">Cat</tt>&#20043;&#21069;&#24050;&#32463;&#34987;<tt class="literal">&#21478;&#19968;Session(secondSession)</tt>&#35013;&#36733;&#20102;&#65292;            &#24212;&#29992;&#31243;&#24207;&#36827;&#34892;&#37325;&#20851;&#32852;&#25805;&#20316;(reattach)&#30340;&#26102;&#20505;&#20250;&#25243;&#20986;&#19968;&#20010;&#24322;&#24120;&#12290;        </p><p>        	&#22914;&#26524;&#20320;&#30830;&#23450;&#24403;&#21069;session&#27809;&#26377;&#21253;&#21547;&#19982;&#20043;&#20855;&#26377;&#30456;&#21516;&#25345;&#20037;&#21270;&#26631;&#35782;&#30340;&#25345;&#20037;&#23454;&#20363;&#65292;&#20351;&#29992;<tt class="literal">update()</tt>&#12290;        	&#22914;&#26524;&#24819;&#38543;&#26102;&#21512;&#24182;&#20320;&#30340;&#30340;&#25913;&#21160;&#32780;&#19981;&#32771;&#34385;session&#30340;&#29366;&#24577;&#65292;&#20351;&#29992;<tt class="literal">merge()</tt>&#12290;	         &#25442;&#21477;&#35805;&#35828;&#65292;&#22312;&#19968;&#20010;&#26032;session&#20013;&#36890;&#24120;&#31532;&#19968;&#20010;&#35843;&#29992;&#30340;&#26159;<tt class="literal">update()</tt>&#26041;&#27861;&#65292;&#20197;&#20415;&#20445;&#35777;&#37325;&#26032;&#20851;&#32852;&#33073;&#31649;(detached)&#23545;&#35937;&#30340;&#25805;&#20316;&#39318;&#20808;&#34987;&#25191;&#34892;&#12290;        </p><p>		    &#24076;&#26395;&#30456;&#20851;&#32852;&#30340;&#33073;&#31649;&#23545;&#35937;&#65288;&#36890;&#36807;&#24341;&#29992;&#8220;&#21487;&#21040;&#36798;&#8221;&#30340;&#33073;&#31649;&#23545;&#35937;&#65289;&#30340;&#25968;&#25454;&#20063;&#35201;&#26356;&#26032;&#21040;&#25968;&#25454;&#24211;&#26102;&#65288;&#24182;&#19988;&#20063;<span class="emphasis"><em>&#20165;&#20165;</em></span>&#22312;&#36825;&#31181;&#24773;&#20917;&#65289;&#65292;		    &#24212;&#29992;&#31243;&#24207;&#38656;&#35201;&#23545;&#35813;&#30456;&#20851;&#32852;&#30340;&#33073;&#31649;&#23545;&#35937;&#21333;&#29420;&#35843;&#29992;<tt class="literal">update()</tt>			&#24403;&#28982;&#36825;&#20123;&#21487;&#20197;&#33258;&#21160;&#23436;&#25104;&#65292;&#21363;&#36890;&#36807;&#20351;&#29992;<span class="emphasis"><em>&#20256;&#25773;&#24615;&#25345;&#20037;&#21270;(transitive persistence)</em></span>&#65292;&#35831;&#30475;<a href="objectstate.html#objectstate-transitive" title="11.11.&nbsp;&#20256;&#25773;&#24615;&#25345;&#20037;&#21270;(transitive persistence)">&#31532;&nbsp;11.11&nbsp;&#33410; &#8220;&#20256;&#25773;&#24615;&#25345;&#20037;&#21270;(transitive persistence)&#8221;</a>&#12290;        </p><p>            <tt class="literal">lock()</tt>&#26041;&#27861;&#20063;&#20801;&#35768;&#31243;&#24207;&#37325;&#26032;&#20851;&#32852;&#26576;&#20010;&#23545;&#35937;&#21040;&#19968;&#20010;&#26032;session&#19978;&#12290;&#19981;&#36807;&#65292;&#35813;&#33073;&#31649;(detached)&#30340;&#23545;&#35937;&#24517;&#39035;&#26159;&#27809;&#26377;&#20462;&#25913;&#36807;&#30340;&#65281;        </p><pre class="programlisting">//just reassociate:sess.lock(fritz, LockMode.NONE);//do a version check, then reassociate:sess.lock(izi, LockMode.READ);//do a version check, using SELECT ... FOR UPDATE, then reassociate:sess.lock(pk, LockMode.UPGRADE);</pre><p>            &#35831;&#27880;&#24847;&#65292;<tt class="literal">lock()</tt>&#21487;&#20197;&#25645;&#37197;&#22810;&#31181;<tt class="literal">LockMode</tt>&#65292;            &#26356;&#22810;&#20449;&#24687;&#35831;&#38405;&#35835;API&#25991;&#26723;&#20197;&#21450;&#20851;&#20110;&#20107;&#21153;&#22788;&#29702;(transaction handling)&#30340;&#31456;&#33410;&#12290;&#37325;&#26032;&#20851;&#32852;&#19981;&#26159;<tt class="literal">lock()</tt>&#30340;&#21807;&#19968;&#29992;&#36884;&#12290;        </p><p>            &#20854;&#20182;&#29992;&#20110;&#38271;&#26102;&#38388;&#24037;&#20316;&#21333;&#20803;&#30340;&#27169;&#22411;&#20250;&#22312;<a href="transactions.html#transactions-optimistic" title="12.3.&nbsp;&#20048;&#35266;&#24182;&#21457;&#25511;&#21046;(Optimistic concurrency control)">&#31532;&nbsp;12.3&nbsp;&#33410; &#8220;&#20048;&#35266;&#24182;&#21457;&#25511;&#21046;(Optimistic concurrency control)&#8221;</a>&#20013;&#35752;&#35770;&#12290;        </p></div><div class="sect1" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="objectstate-saveorupdate"></a>11.7.&nbsp;&#33258;&#21160;&#29366;&#24577;&#26816;&#27979;</h2></div></div><div></div></div><p>            Hibernate&#30340;&#29992;&#25143;&#26366;&#35201;&#27714;&#19968;&#20010;&#26082;&#21487;&#33258;&#21160;&#20998;&#37197;&#26032;&#25345;&#20037;&#21270;&#26631;&#35782;(identifier)&#20445;&#23384;&#30636;&#26102;(transient)&#23545;&#35937;&#65292;&#21448;&#21487;&#26356;&#26032;/&#37325;&#26032;&#20851;&#32852;&#33073;&#31649;(detached)&#23454;&#20363;&#30340;&#36890;&#29992;&#26041;&#27861;&#12290;            <tt class="literal">saveOrUpdate()</tt>&#26041;&#27861;&#23454;&#29616;&#20102;&#36825;&#20010;&#21151;&#33021;&#12290;        </p><pre class="programlisting">// in the first sessionCat cat = (Cat) firstSession.load(Cat.class, catID);// in a higher tier of the applicationCat mate = new Cat();cat.setMate(mate);// later, in a new sessionsecondSession.saveOrUpdate(cat);   // update existing state (cat has a non-null id)secondSession.saveOrUpdate(mate);  // save the new instance (mate has a null id)</pre><p>            <tt class="literal">saveOrUpdate()</tt>&#29992;&#36884;&#21644;&#35821;&#20041;&#21487;&#33021;&#20250;&#20351;&#26032;&#29992;&#25143;&#24863;&#21040;&#36855;&#24785;&#12290;			&#39318;&#20808;&#65292;&#21482;&#35201;&#20320;&#27809;&#26377;&#23581;&#35797;&#22312;&#26576;&#20010;session&#20013;&#20351;&#29992;&#26469;&#33258;&#21478;&#19968;session&#30340;&#23454;&#20363;&#65292;&#20320;&#24212;&#35813;&#23601;&#19981;&#38656;&#35201;&#20351;&#29992;<tt class="literal">update()</tt>&#65292;            <tt class="literal">saveOrUpdate()</tt>&#65292;&#25110;<tt class="literal">merge()</tt>&#12290;&#26377;&#20123;&#31243;&#24207;&#20174;&#26469;&#19981;&#29992;&#36825;&#20123;&#26041;&#27861;&#12290;        </p><p>            &#36890;&#24120;&#19979;&#38754;&#30340;&#22330;&#26223;&#20250;&#20351;&#29992;<tt class="literal">update()</tt>&#25110;<tt class="literal">saveOrUpdate()</tt>&#65306;        </p><div class="itemizedlist"><ul type="disc" compact><li><p>                    &#31243;&#24207;&#22312;&#31532;&#19968;&#20010;session&#20013;&#21152;&#36733;&#23545;&#35937;                </p></li><li><p>                    &#35813;&#23545;&#35937;&#34987;&#20256;&#36882;&#21040;&#34920;&#29616;&#23618;                </p></li><li><p>

⌨️ 快捷键说明

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