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

📄 manipulatingdata.html

📁 图象处理
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<html><head>      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">   <title>&#31532;&nbsp;9&nbsp;&#31456;&nbsp;&#25805;&#20316;&#25345;&#20037;&#21270;&#25968;&#25454;(Manipulating Persistent Data)</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="inheritance.html" title="&#31532;&nbsp;8&nbsp;&#31456;&nbsp;&#32487;&#25215;&#26144;&#23556;(Inheritance Mappings)"><link rel="next" href="transactions.html" title="&#31532;&nbsp;10&nbsp;&#31456;&nbsp;&#20107;&#21153;&#21644;&#24182;&#34892;&#65288;Transactions And Concurrency&#65289;"></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;9&nbsp;&#31456;&nbsp;&#25805;&#20316;&#25345;&#20037;&#21270;&#25968;&#25454;(Manipulating Persistent Data)</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="inheritance.html">&#19978;&#19968;&#39029;</a>&nbsp;</td><th width="60%" align="center">&nbsp;</th><td width="20%" align="right">&nbsp;<a accesskey="n" href="transactions.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="manipulatingdata"></a>&#31532;&nbsp;9&nbsp;&#31456;&nbsp;&#25805;&#20316;&#25345;&#20037;&#21270;&#25968;&#25454;(Manipulating Persistent Data)</h2></div></div><div></div></div><div class="sect1" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="manipulatingdata-creating"></a>9.1.&nbsp;&#21019;&#24314;&#19968;&#20010;&#25345;&#20037;&#21270;&#23545;&#35937;</h2></div></div><div></div></div><p>            &#23545;&#35937;&#65288;&#23454;&#20307;&#30340;&#23454;&#20363;&#65289;&#23545;&#19968;&#20010;&#29305;&#23450;&#30340;<tt class="literal">Session</tt>&#26469;&#35828;&#65292;&#35201;&#20040;&#26159;&#19968;&#20010;<span class="emphasis"><em>&#30636;&#26102;&#65288;transient&#65289;</em></span>&#23545;&#35937;&#65292;&#35201;&#20040;&#26159;<tt class="literal">&#25345;&#20037;&#21270;&#65288;persistent&#65289;</tt>&#23545;&#35937;&#12290;&#21018;&#21018;&#21019;&#24314;&#30340;&#23545;&#35937;&#24403;&#28982;&#26159;&#30636;&#26102;&#30340;(&#27880;&#65306;&#21518;&#25991;&#20013;transient object&#20063;&#31216;&#20026;&#20020;&#26102;&#23545;&#35937;)&#12290;session&#21017;&#25552;&#20379;&#20102;&#25226;&#30636;&#26102;&#23454;&#20363;&#20445;&#23384;&#65288;&#25345;&#20037;&#21270;&#65289;&#30340;&#26381;&#21153;:        </p><pre class="programlisting">DomesticCat fritz = new DomesticCat();fritz.setColor(Color.GINGER);fritz.setSex('M');fritz.setName("Fritz");Long generatedId = (Long) sess.save(fritz);</pre><pre class="programlisting">DomesticCat pk = new DomesticCat();pk.setColor(Color.TABBY);pk.setSex('F');pk.setName("PK");pk.setKittens( new HashSet() );pk.addKitten(fritz);sess.save( pk, new Long(1234) );</pre><p>            &#21333;&#21442;&#25968;&#30340;<tt class="literal">save()</tt>&#26041;&#27861;&#20026;<tt class="literal">fritz</tt>&#29983;&#25104;&#20102;&#19968;&#20010;&#24799;&#19968;&#26631;&#35782;&#31526;&#65292;&#24182;&#36171;&#32473;&#36825;&#20010;&#23545;&#35937;&#12290;&#21452;&#21442;&#25968;&#30340;&#24418;&#24335;&#21017;&#20351;&#29992;&#32473;&#23450;&#30340;&#26631;&#35782;&#31526;&#20445;&#23384;<tt class="literal">pk</tt>&#12290;&#25105;&#20204;&#19968;&#33324;&#19981;&#40723;&#21169;&#20351;&#29992;&#21452;&#21442;&#25968;&#30340;&#24418;&#24335;&#65292;&#22240;&#20026;&#36825;&#21487;&#33021;&#20250;&#65288;&#38544;&#21547;&#65289;&#20351;&#20027;&#38190;&#36171;&#20104;&#19994;&#21153;&#21547;&#20041;&#12290;&#23427;&#26377;&#29992;&#30340;&#26102;&#20505;&#26159;&#22312;&#19968;&#20123;&#29305;&#27530;&#22330;&#21512;&#19979;&#65292;&#27604;&#22914;&#20351;&#29992;Hibernate&#26469;&#25345;&#20037;&#21270;&#19968;&#20010;BMP&#23454;&#20307;bean.        </p><p>            &#20851;&#32852;&#30340;&#23545;&#35937;&#21487;&#20197;&#29992;&#20320;&#21916;&#27426;&#30340;&#20219;&#20309;&#39034;&#24207;&#25345;&#20037;&#21270;&#65292;&#38500;&#38750;&#26377;&#22806;&#38190;&#23383;&#27573;&#20855;&#26377;<tt class="literal">NOT NULL</tt>&#30340;&#32422;&#26463;&#12290;&#20915;&#19981;&#20250;&#26377;&#22806;&#38190;&#32422;&#26463;&#20914;&#31361;&#30340;&#21361;&#38505;&#12290;&#28982;&#32780;&#65292;&#22914;&#26524;&#22312;<tt class="literal">save()</tt>&#23545;&#35937;&#30340;&#26102;&#20505;&#29992;&#38169;&#20102;&#39034;&#24207;&#65292;&#20250;&#35302;&#29359;<tt class="literal">NOT NULL</tt>&#32422;&#26463;&#12290;        </p></div><div class="sect1" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="manipulatingdata-loading"></a>9.2.&nbsp;&#35013;&#36733;&#23545;&#35937;</h2></div></div><div></div></div><p>            &#22914;&#26524;&#20320;&#24050;&#30693;&#26576;&#20010;&#25345;&#20037;&#21270;&#23454;&#20363;&#30340;&#26631;&#35782;&#31526;&#65292;<tt class="literal">Session</tt>&#30340;<tt class="literal">load()</tt>&#26041;&#27861;&#35753;&#20320;&#21462;&#20986;&#23427;&#12290;&#31532;&#19968;&#31181;&#24418;&#24335;&#20351;&#29992;&#19968;&#20010;&#31867;&#23545;&#35937;&#20316;&#20026;&#21442;&#25968;&#65292;&#20250;&#25226;&#29366;&#24577;&#35013;&#36733;&#21040;&#21478;&#19968;&#20010;&#26032;&#21019;&#24314;&#30340;&#23545;&#35937;&#20013;&#21435;&#12290;&#31532;&#20108;&#20010;&#29256;&#26412;&#20801;&#35768;&#20320;&#32473;&#20986;&#19968;&#20010;&#23454;&#20363;&#65292;&#20250;&#22312;&#20854;&#20013;&#35013;&#36733;&#29366;&#24577;&#12290;&#25226;&#23454;&#20363;&#20316;&#20026;&#21442;&#25968;&#30340;&#24418;&#24335;&#22312;&#20320;&#20934;&#22791;&#25226;Hibernate&#21644;BMP&#23454;&#20307;bean&#19968;&#36215;&#20351;&#29992;&#30340;&#26102;&#20505;&#29305;&#21035;&#26377;&#29992;&#65292;&#23427;&#23601;&#26159;&#20026;&#27492;&#35774;&#35745;&#30340;&#12290;&#20320;&#20063;&#21487;&#20197;&#21457;&#29616;&#20854;&#20182;&#30340;&#29992;&#36884;&#65288;&#27604;&#22914;&#33258;&#24049;&#23454;&#29616;&#23454;&#20363;&#27744;&#31561;&#31561;&#65289;&#12290;        </p><pre class="programlisting">Cat fritz = (Cat) sess.load(Cat.class, generatedId);</pre><pre class="programlisting">// you need to wrap primitive identifierslong pkId = 1234;DomesticCat pk = (DomesticCat) sess.load( Cat.class, new Long(pkId) );</pre><pre class="programlisting">Cat cat = new DomesticCat();// load pk''s state into catsess.load( cat, new Long(pkId) );Set kittens = cat.getKittens();</pre><p>            &#35831;&#27880;&#24847;&#22914;&#26524;&#27809;&#26377;&#21305;&#37197;&#30340;&#25968;&#25454;&#24211;&#35760;&#24405;&#65292;<tt class="literal">load()</tt>&#26041;&#27861;&#21487;&#33021;&#25243;&#20986;&#26080;&#27861;&#24674;&#22797;&#30340;exception&#12290;&#22914;&#26524;&#31867;&#26159;&#36890;&#36807;&#20195;&#29702;&#26144;&#23556;&#30340;&#65292;<tt class="literal">load()</tt>&#26041;&#27861;&#36820;&#22238;&#19968;&#20010;&#23545;&#35937;&#65292;&#36825;&#26159;&#19968;&#20010;&#26410;&#21021;&#22987;&#21270;&#30340;&#20195;&#29702;&#65292;&#24182;&#19988;&#30452;&#21040;&#20320;&#35843;&#29992;&#35813;&#23545;&#35937;&#30340;&#26576;&#26041;&#27861;&#26102;&#25165;&#20250;&#21435;&#35775;&#38382;&#25968;&#25454;&#24211;&#12290;&#36825;&#31181;&#34892;&#20026;&#26041;&#24335;&#22312;&#20320;&#21916;&#27426;&#21019;&#24314;&#19968;&#20010;&#25351;&#21521;&#26576;&#23545;&#35937;&#30340;&#20851;&#32852;&#65292;&#21448;&#19981;&#24819;&#30495;&#30340;&#20174;&#25968;&#25454;&#24211;&#20013;&#35013;&#36733;&#23427;&#30340;&#26102;&#20505;&#29305;&#21035;&#26377;&#29992;&#12290;        </p><p>            &#22914;&#26524;&#20320;&#19981;&#30830;&#23450;&#26159;&#21542;&#26377;&#21305;&#37197;&#30340;&#34892;&#23384;&#22312;&#65292;&#20320;&#24212;&#35813;&#20351;&#29992;<tt class="literal">get()</tt>&#26041;&#27861;&#65292;&#23427;&#20250;&#31435;&#21051;&#35775;&#38382;&#25968;&#25454;&#24211;&#65292;&#22914;&#26524;&#27809;&#26377;&#23545;&#24212;&#30340;&#34892;&#65292;&#36820;&#22238;null&#12290;        </p><pre class="programlisting">Cat cat = (Cat) sess.get(Cat.class, id);if (cat==null) {    cat = new Cat();    sess.save(cat, id);}return cat;</pre><p>            &#20320;&#21487;&#20197;&#29992;SQL<tt class="literal">SELECT ... FOR UPDATE</tt>&#35013;&#36733;&#23545;&#35937;&#12290;&#19979;&#19968;&#33410;&#26377;&#20851;&#20110;Hibernate <tt class="literal">LockMode</tt>&#30340;&#35752;&#35770;&#12290;        </p><pre class="programlisting">Cat cat = (Cat) sess.get(Cat.class, id, LockMode.UPGRADE);</pre><p>            &#27880;&#24847;&#65292;&#20219;&#20309;&#20851;&#32852;&#30340;&#23454;&#20363;&#25110;&#32773;&#21253;&#21547;&#30340;&#38598;&#21512;&#37117;<span class="emphasis"><em>&#19981;&#20250;</em></span>&#34987;&#20570;&#20026;<tt class="literal">FOR UPDATE</tt>&#36820;&#22238;&#12290;        </p><p>            &#20219;&#20309;&#26102;&#20505;&#37117;&#21487;&#20197;&#20351;&#29992;<tt class="literal">refresh()</tt>&#26041;&#27861;&#37325;&#26032;&#35013;&#36733;&#23545;&#35937;&#21644;&#23427;&#30340;&#38598;&#21512;&#12290;&#22914;&#26524;&#20320;&#20351;&#29992;&#25968;&#25454;&#24211;&#35302;&#21457;&#22120;&#26356;&#25913;&#20102;&#23545;&#35937;&#30340;&#26576;&#20123;&#23646;&#24615;&#65292;&#36825;&#23601;&#24456;&#26377;&#29992;&#12290;        </p><pre class="programlisting">sess.save(cat);sess.flush(); //force the SQL INSERTsess.refresh(cat); //re-read the state (after the trigger executes)</pre></div><div class="sect1" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="manipulatingdata-querying"></a>9.3.&nbsp;Querying</h2></div></div><div></div></div><p>            &#22914;&#26524;&#20320;&#19981;&#33021;&#30830;&#23450;&#20320;&#35201;&#23547;&#25214;&#30340;&#23545;&#35937;&#30340;&#26631;&#31034;&#31526;&#65292;&#35831;&#20351;&#29992;<tt class="literal">Session</tt>&#30340;<tt class="literal">find()</tt>&#26041;&#27861;&#12290;Hibernate&#20351;&#29992;&#19968;&#31181;&#31616;&#21333;&#32780;&#24378;&#22823;&#30340;&#38754;&#21521;&#23545;&#35937;&#26597;&#35810;&#35821;&#35328;&#12290;        </p><pre class="programlisting">List cats = sess.find(    "from Cat as cat where cat.birthdate = ?",    date,    Hibernate.DATE);List mates = sess.find(    "select mate from Cat as cat join cat.mate as mate " +    "where cat.name = ?",    name,    Hibernate.STRING);List cats = sess.find( "from Cat as cat where cat.mate.bithdate is null" );List moreCats = sess.find(    "from Cat as cat where " +     "cat.name = 'Fritz' or cat.id = ? or cat.id = ?",    new Object[] { id1, id2 },    new Type[] { Hibernate.LONG, Hibernate.LONG });List mates = sess.find(    "from Cat as cat where cat.mate = ?",    izi,    Hibernate.entity(Cat.class));List problems = sess.find(    "from GoldFish as fish " +    "where fish.birthday &gt; fish.deceased or fish.birthday is null");</pre><p>           	<tt class="literal">find()</tt>&#30340;&#31532;&#20108;&#20010;&#21442;&#25968;&#25509;&#21463;&#19968;&#20010;&#23545;&#35937;&#25110;&#32773;&#23545;&#35937;&#25968;&#32452;&#12290;&#31532;&#19977;&#20010;&#21442;&#25968;&#25509;&#21463;&#19968;&#20010;Hibernate&#31867;&#22411;&#25110;&#32773;&#31867;&#22411;&#30340;&#25968;&#32452;&#12290;&#36825;&#20123;&#25351;&#23450;&#30340;&#31867;&#22411;&#29992;&#26469;&#25226;&#32473;&#23450;&#30340;&#23545;&#35937;&#32465;&#23450;&#21040;&#26597;&#35810;&#20013;&#30340;<tt class="literal">?</tt>&#21344;&#20301;&#31526;&#65288;&#23454;&#38469;&#19978;&#23545;&#24212;&#30340;&#26159;JDBC <tt class="literal">PreparedStatement</tt>&#30340;&#20256;&#20837;&#21442;&#25968;&#65289;&#12290;&#23601;&#20687;&#22312;JDBC&#20013;&#19968;&#30524;&#65292;&#20320;&#24212;&#35813;&#20248;&#20808;&#20351;&#29992;&#36825;&#31181;&#21442;&#25968;&#32465;&#23450;&#30340;&#26041;&#24335;&#65292;&#32780;&#38750;&#32452;&#35013;&#23383;&#31526;&#20018;&#12290;        </p><p>            <tt class="literal">Hibernate</tt>&#31867;&#23450;&#20041;&#20102;&#19968;&#20123;&#38745;&#24577;&#26041;&#27861;&#21644;&#24120;&#37327;&#65292;&#25552;&#20379;&#20102;&#35775;&#38382;&#22823;&#37096;&#20998;&#20869;&#32622;&#31867;&#22411;&#30340;&#25163;&#27573;&#12290;&#36825;&#20123;&#20869;&#32622;&#31867;&#22411;&#26159;<tt class="literal">net.sf.hibernate.type.Type</tt>&#30340;&#23454;&#20363;&#12290;        </p><p>            &#22914;&#26524;&#20320;&#30693;&#36947;&#20320;&#30340;&#26597;&#35810;&#20250;&#36820;&#22238;&#38750;&#24120;&#22823;&#37327;&#30340;&#23545;&#35937;&#65292;&#20294;&#26159;&#20320;&#19981;&#24076;&#26395;&#20840;&#37096;&#20351;&#29992;&#23427;&#20204;&#65292;&#20320;&#21487;&#20197;&#29992;<tt class="literal">iterate()</tt>&#26041;&#27861;&#33719;&#24471;&#26356;&#22909;&#30340;&#24615;&#33021;&#65292;&#23427;&#20250;&#36820;&#22238;&#19968;&#20010;<tt class="literal">java.util.Iterator</tt>&#12290;&#36825;&#20010;&#36845;&#20195;&#22120;&#20250;&#22312;&#38656;&#35201;&#30340;&#26102;&#20505;&#35013;&#36733;&#23545;&#35937;&#65292;&#25152;&#20351;&#29992;&#30340;&#26631;&#35782;&#31526;&#26469;&#33258;&#19968;&#20010;&#21069;&#23548;&#30340;SQL&#26597;&#35810;&#12290;(&#19968;&#20849;&#26159;N+1&#27425;&#26597;&#35810;&#65289;        </p><pre class="programlisting">// fetch idsIterator iter = sess.iterate("from eg.Qux q order by q.likeliness"); while ( iter.hasNext() ) {    Qux qux = (Qux) iter.next();  // fetch the object    // something we couldnt express in the query    if ( qux.calculateComplicatedAlgorithm() ) {        // delete the current instance        iter.remove();        // dont need to process the rest        break;    }}</pre><p>        	&#24456;&#19981;&#24184;&#65292;<tt class="literal">java.util.Iterator</tt>&#27809;&#26377;&#22768;&#26126;&#20219;&#20309;exception&#12290;&#25152;&#20197;&#65292;&#21457;&#29983;&#30340;&#20219;&#20309;SQL&#25110;&#32773;Hibernate&#30340;exception&#37117;&#20250;&#34987;&#21253;&#35013;&#22312;&#19968;&#20010;<tt class="literal">LazyInitializationException</tt>&#20013;&#65288;&#23427;&#26159;<tt class="literal">RuntimeException</tt>&#30340;&#23376;&#31867;&#65289;&#12290;        </p><p>            &#22914;&#26524;&#20320;&#39044;&#26399;&#22823;&#37096;&#20998;&#30340;&#23545;&#35937;&#24050;&#32463;&#35013;&#36733;&#36807;&#65292;&#23384;&#22312;&#20110;session&#30340;&#32531;&#23384;&#20013;&#20102;&#65292;&#25110;&#32773;&#26597;&#35810;&#32467;&#26524;&#21253;&#21547;&#21516;&#26679;&#30340;&#23545;&#35937;&#24456;&#22810;&#27425;&#65292;&#37027;&#20040;<tt class="literal">iterator()</tt>&#26041;&#27861;&#20063;&#20250;&#33719;&#24471;&#26356;&#22909;&#30340;&#24615;&#33021;&#12290;&#65288;&#22914;&#26524;&#27809;&#26377;&#20219;&#20309;&#25968;&#25454;&#34987;&#32531;&#23384;&#25110;&#32773;&#37325;&#22797;&#20986;&#29616;&#65292;&#21017;<tt class="literal">find()</tt>&#24635;&#26159;&#20250;&#26356;&#24555;&#12290;&#65289;&#19979;&#38754;&#26159;&#19968;&#20010;&#24212;&#35813;&#20351;&#29992;<tt class="literal">iterator()</tt>&#35843;&#29992;&#30340;&#26597;&#35810;&#20363;&#23376;&#65306;        </p><pre class="programlisting">Iterator iter = sess.iterate(    "select customer, product " + 

⌨️ 快捷键说明

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