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

📄 queryhql.html

📁 图象处理
💻 HTML
📖 第 1 页 / 共 4 页
字号:
<html><head>      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">   <title>&#31532;&nbsp;11&nbsp;&#31456;&nbsp;Hibernate&#26597;&#35810;&#35821;&#35328;(Query Language), &#21363;HQL</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="transactions.html" title="&#31532;&nbsp;10&nbsp;&#31456;&nbsp;&#20107;&#21153;&#21644;&#24182;&#34892;&#65288;Transactions And Concurrency&#65289;"><link rel="next" href="querycriteria.html" title="&#31532;&nbsp;12&nbsp;&#31456;&nbsp;&#26465;&#20214;&#26597;&#35810;(Criteria Query)"></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;11&nbsp;&#31456;&nbsp;Hibernate&#26597;&#35810;&#35821;&#35328;(Query Language), &#21363;HQL</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="transactions.html">&#19978;&#19968;&#39029;</a>&nbsp;</td><th width="60%" align="center">&nbsp;</th><td width="20%" align="right">&nbsp;<a accesskey="n" href="querycriteria.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="queryhql"></a>&#31532;&nbsp;11&nbsp;&#31456;&nbsp;Hibernate&#26597;&#35810;&#35821;&#35328;(Query Language), &#21363;HQL</h2></div></div><div></div></div><p>    Hibernate&#35013;&#22791;&#20102;&#19968;&#31181;&#26497;&#20026;&#26377;&#21147;&#30340;&#26597;&#35810;&#35821;&#35328;&#65292;&#65288;&#26377;&#24847;&#22320;&#65289;&#30475;&#19978;&#21435;&#24456;&#20687;SQL&#12290;&#20294;&#26159;&#21035;&#34987;&#35821;&#27861;&#33945;&#34109;&#65292;HQL&#26159;&#23436;&#20840;&#38754;&#21521;&#23545;&#35937;&#30340;&#65292;&#20855;&#22791;&#32487;&#25215;&#12289;&#22810;&#24577;&#21644;&#20851;&#32852;&#31561;&#29305;&#24615;&#12290;    </p><div class="sect1" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="queryhql-casesensitivity"></a>11.1.&nbsp;&#22823;&#23567;&#20889;&#25935;&#24863;&#24615;(Case Sensitivity)</h2></div></div><div></div></div><p>            &#38500;&#20102;Java&#31867;&#21644;&#23646;&#24615;&#21517;&#31216;&#22806;&#65292;&#26597;&#35810;&#37117;&#26159;&#22823;&#23567;&#20889;&#19981;&#25935;&#24863;&#30340;&#12290;            &#25152;&#20197;&#65292; <tt class="literal">SeLeCT</tt> &#21644;            <tt class="literal">sELEct</tt> &#20197;&#21450;            <tt class="literal">SELECT</tt> &#30456;&#21516;&#30340;&#65292;&#20294;&#26159;            <tt class="literal">net.sf.hibernate.eg.FOO</tt> &#21644;            <tt class="literal">net.sf.hibernate.eg.Foo</tt> &#26159;&#19981;&#21516;&#30340;&#65292;            <tt class="literal">foo.barSet</tt> &#21644;            <tt class="literal">foo.BARSET</tt>&#20063;&#26159;&#19981;&#21516;&#30340;&#12290;        </p><p>            &#26412;&#25163;&#20876;&#20351;&#29992;&#23567;&#20889;&#30340;HQL&#20851;&#38190;&#35789;&#12290;&#26377;&#20123;&#29992;&#25143;&#35748;&#20026;&#22312;&#26597;&#35810;&#20013;&#20351;&#29992;&#22823;&#20889;&#30340;&#20851;&#38190;&#23383;&#26356;&#21152;&#26131;&#35835;&#65292;&#20294;&#26159;&#25105;&#20204;&#35748;&#20026;&#23884;&#20837;&#22312;Java&#20195;&#30721;&#20013;&#36825;&#26679;&#24456;&#38590;&#30475;&#12290;                    </p></div><div class="sect1" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="queryhql-from"></a>11.2.&nbsp;from &#23376;&#21477;</h2></div></div><div></div></div><p>            &#21487;&#33021;&#26368;&#31616;&#21333;&#30340;Hibernate&#26597;&#35810;&#26159;&#36825;&#26679;&#30340;&#24418;&#24335;&#65306;        </p><pre class="programlisting">from eg.Cat</pre><p>            &#23427;&#31616;&#21333;&#30340;&#36820;&#22238;&#25152;&#26377;<tt class="literal">eg.Cat</tt>&#31867;&#30340;&#23454;&#20363;&#12290;        </p><p>            &#22823;&#37096;&#20998;&#24773;&#20917;&#19979;&#65292;&#20320;&#38656;&#35201;&#36171;&#20104;&#23427;&#19968;&#20010;<span class="emphasis"><em>&#21035;&#21517;&#65288;alias&#65289;</em></span>&#65292;&#22240;&#20026;&#20320;&#22312;&#26597;&#35810;&#30340;&#20854;&#20182;&#22320;&#26041;&#20063;&#20250;&#24341;&#29992;&#36825;&#20010;<tt class="literal">Cat</tt>&#12290;        </p><pre class="programlisting">from eg.Cat as cat</pre><p>            &#19978;&#38754;&#30340;&#35821;&#21477;&#20026;<tt class="literal">Cat</tt>&#36171;&#20104;&#20102;&#19968;&#20010;&#21035;&#21517;<tt class="literal">cat</tt> &#12290;&#25152;&#20197;&#21518;&#38754;&#30340;&#26597;&#35810;&#21487;&#20197;&#29992;&#36825;&#20010;&#31616;&#21333;&#30340;&#21035;&#21517;&#20102;&#12290;<tt class="literal">as</tt>&#20851;&#38190;&#23383;&#26159;&#21487;&#20197;&#30465;&#30053;&#30340;&#65292;&#25105;&#20204;&#20063;&#21487;&#20197;&#20889;&#25104;&#36825;&#26679;&#65306;        </p><pre class="programlisting">from eg.Cat cat</pre><p>            &#21487;&#20197;&#20986;&#29616;&#22810;&#20010;&#31867;&#65292;&#32467;&#26524;&#26159;&#23427;&#20204;&#30340;&#31515;&#21345;&#23572;&#31215;&#65292;&#25110;&#32773;&#31216;&#20026;&#8220;&#20132;&#21449;&#8221;&#36830;&#25509;&#12290;        </p><pre class="programlisting">from Formula, Parameter</pre><pre class="programlisting">from Formula as form, Parameter as param</pre><p>            &#35753;&#26597;&#35810;&#20013;&#30340;&#21035;&#21517;&#26381;&#20174;&#39318;&#23383;&#27597;&#23567;&#20889;&#30340;&#35268;&#21017;&#65292;&#25105;&#20204;&#35748;&#20026;&#36825;&#26159;&#19968;&#20010;&#22909;&#20064;&#24815;&#12290;&#36825;&#21644;Java&#23545;&#23616;&#37096;&#21464;&#37327;&#30340;&#21629;&#21517;&#35268;&#33539;&#26159;&#19968;&#33268;&#30340;&#12290;(&#27604;&#22914;&#65292;<tt class="literal">domesticCat</tt>).        </p></div><div class="sect1" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="queryhql-joins"></a>11.3.&nbsp;&#32852;&#21512;&#65288;Associations&#65289;&#21644;&#36830;&#25509;&#65288;joins&#65289;</h2></div></div><div></div></div><p>            &#20320;&#21487;&#20197;&#20351;&#29992;<tt class="literal">join</tt>&#23450;&#20041;&#20004;&#20010;&#23454;&#20307;&#30340;&#36830;&#25509;&#65292;&#21516;&#26102;&#25351;&#26126;&#21035;&#21517;&#12290;        </p><pre class="programlisting">from eg.Cat as cat     inner join cat.mate as mate    left outer join cat.kittens as kittenfrom eg.Cat as cat left join cat.mate.kittens as kittensfrom Formula form full join form.parameter param</pre><p>            &#25903;&#25345;&#30340;&#36830;&#25509;&#31867;&#22411;&#26159;&#20174;ANSI SQL&#20511;&#29992;&#30340;&#65306;        </p><div class="itemizedlist"><ul type="disc" compact><li><p>                    <tt class="literal">&#20869;&#36830;&#25509;&#65292;inner join</tt>                </p></li><li><p>                    <tt class="literal">&#24038;&#22806;&#36830;&#25509;&#65292;left outer join</tt>                </p></li><li><p>                    <tt class="literal">&#21491;&#22806;&#36830;&#25509;&#65292;right outer join</tt>                </p></li><li><p>                    <tt class="literal">&#20840;&#36830;&#25509;&#65292;full join</tt> (&#19981;&#24120;&#20351;&#29992;)                </p></li></ul></div><p>            <tt class="literal">inner join</tt>, <tt class="literal">left outer join</tt> &#21644;            <tt class="literal">right outer join</tt> &#37117;&#21487;&#20197;&#31616;&#20889;&#12290;        </p><pre class="programlisting">from eg.Cat as cat     join cat.mate as mate    left join cat.kittens as kitten</pre><p>            &#24182;&#19988;&#65292;&#21152;&#19978; "fetch"&#21518;&#32512;&#30340;&#25235;&#21462;&#36830;&#25509;&#21487;&#20197;&#35753;&#32852;&#21512;&#30340;&#23545;&#35937;&#38543;&#30528;&#23427;&#20204;&#30340;&#29238;&#23545;&#35937;&#30340;&#21021;&#22987;&#21270;&#32780;&#21021;&#22987;&#21270;&#65292;&#21482;&#38656;&#35201;&#19968;&#20010;select&#35821;&#21477;&#12290;&#36825;&#22312;&#21021;&#22987;&#21270;&#19968;&#20010;&#38598;&#21512;&#30340;&#26102;&#20505;&#29305;&#21035;&#26377;&#29992;&#12290;&#23427;&#26377;&#25928;&#22320;&#35206;&#30422;&#20102;&#26144;&#23556;&#25991;&#20214;&#20013;&#23545;&#20851;&#32852;&#21644;&#38598;&#21512;&#30340;&#22806;&#36830;&#25509;&#23450;&#20041;&#12290;        </p><pre class="programlisting">from eg.Cat as cat     inner join fetch cat.mate    left join fetch cat.kittens</pre><p>            &#25235;&#21462;&#36830;&#25509;&#19968;&#33324;&#19981;&#38656;&#35201;&#36171;&#20104;&#21035;&#21517;&#65292;&#22240;&#20026;&#34987;&#32852;&#21512;&#30340;&#23545;&#35937;&#24212;&#35813;&#19981;&#20250;&#22312;<tt class="literal">where</tt>&#23376;&#21477;&#65288;&#25110;&#32773;&#20219;&#20309;&#20854;&#23427;&#23376;&#21477;&#65289;&#20013;&#20986;&#29616;&#12290;&#24182;&#19988;&#65292;&#34987;&#32852;&#21512;&#30340;&#23545;&#35937;&#20063;&#19981;&#20250;&#22312;&#26597;&#35810;&#32467;&#26524;&#20013;&#30452;&#25509;&#20986;&#29616;&#12290;&#23427;&#20204;&#26159;&#36890;&#36807;&#29238;&#23545;&#35937;&#36827;&#34892;&#35775;&#38382;&#30340;&#12290;        </p><p>            &#35831;&#27880;&#24847;&#65292;&#30446;&#21069;&#30340;&#23454;&#29616;&#20013;&#65292;&#22312;&#19968;&#27425;&#26597;&#35810;&#20013;&#21482;&#20250;&#25235;&#21462;&#19968;&#20010;&#38598;&#21512;&#65288;&#20854;&#20182;&#30340;&#19968;&#20999;&#37117;&#20570;&#19981;&#21040;&#12290;&#65289;&#65288;&#65311;&#21407;&#25991;&#20026;&#65306;only one collection role may be fetched            in a query&#65289;&#12290;&#20063;&#35831;&#27880;&#24847;&#65292;&#22312;&#20351;&#29992;<tt class="literal">scroll()</tt>&#25110;&#32773; <tt class="literal">iterate()</tt>&#26041;&#24335;&#35843;&#29992;&#30340;&#26597;&#35810;&#20013;&#65292;&#26159;&#31105;&#27490;&#20351;&#29992;<tt class="literal">fetch</tt>&#26500;&#36896;&#30340;&#12290;&#26368;&#21518;&#65292;&#35831;&#27880;&#24847;<tt class="literal">full join fetch</tt>&#21644;<tt class="literal">right join  fetch</tt>&#26159;&#27809;&#26377;&#24847;&#20041;&#30340;&#12290;        </p></div><div class="sect1" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="queryhql-select"></a>11.4.&nbsp;select&#23376;&#21477;</h2></div></div><div></div></div><p>            <tt class="literal">select</tt>&#23376;&#21477;&#36873;&#25321;&#22312;&#32467;&#26524;&#38598;&#20013;&#36820;&#22238;&#21738;&#20123;&#23545;&#35937;&#21644;&#23646;&#24615;&#12290;&#24605;&#32771;&#19968;&#19979;&#19979;&#38754;&#30340;&#20363;&#23376;&#65306;        </p><pre class="programlisting">select mate from eg.Cat as cat     inner join cat.mate as mate</pre><p>            &#36825;&#20010;&#26597;&#35810;&#20250;&#36873;&#25321;&#20986;&#20316;&#20026;&#20854;&#23427;&#29483;&#65288;<tt class="literal">Cat</tt>&#65289;&#26379;&#21451;&#65288;<tt class="literal">mate</tt>&#65289;&#30340;&#37027;&#20123;&#29483;&#12290;&#24403;&#28982;&#65292;&#20320;&#21487;&#20197;&#26356;&#21152;&#30452;&#25509;&#30340;&#20889;&#25104;&#19979;&#38754;&#30340;&#24418;&#24335;&#65306;        </p><pre class="programlisting">select cat.mate from eg.Cat cat</pre><p>            &#20320;&#29978;&#33267;&#21487;&#20197;&#36873;&#25321;&#38598;&#21512;&#20803;&#32032;&#65292;&#20351;&#29992;&#29305;&#27530;&#30340;<tt class="literal">elements</tt>&#21151;&#33021;&#12290;&#19979;&#38754;&#30340;&#26597;&#35810;&#36820;&#22238;&#25152;&#26377;&#29483;&#30340;&#23567;&#29483;&#12290;        </p><pre class="programlisting">select elements(cat.kittens) from eg.Cat cat</pre><p>            &#26597;&#35810;&#21487;&#20197;&#36820;&#22238;&#20219;&#20309;&#20540;&#31867;&#22411;&#30340;&#23646;&#24615;&#65292;&#21253;&#25324;&#32452;&#20214;&#31867;&#22411;&#30340;&#23646;&#24615;&#65306;        </p><pre class="programlisting">select cat.name from eg.DomesticCat catwhere cat.name like 'fri%'select cust.name.firstName from Customer as cust</pre><p>            &#26597;&#35810;&#21487;&#20197;&#29992;&#20803;&#32032;&#31867;&#22411;&#26159;<tt class="literal">Object[]</tt>&#30340;&#19968;&#20010;&#25968;&#32452;&#36820;&#22238;&#22810;&#20010;&#23545;&#35937;&#21644;/&#25110;&#22810;&#20010;&#23646;&#24615;&#12290;        </p><pre class="programlisting">select mother, offspr, mate.name from eg.DomesticCat as mother    inner join mother.mate as mate    left outer join mother.kittens as offspr</pre><p>            &#25110;&#32773;&#23454;&#38469;&#19978;&#26159;&#31867;&#22411;&#23433;&#20840;&#30340;Java&#23545;&#35937;        </p><pre class="programlisting">select new Family(mother, mate, offspr)from eg.DomesticCat as mother    join mother.mate as mate    left join mother.kittens as offspr</pre><p>            &#19978;&#38754;&#30340;&#20195;&#30721;&#20551;&#23450;<tt class="literal">Family</tt>&#26377;&#19968;&#20010;&#21512;&#36866;&#30340;&#26500;&#36896;&#20989;&#25968;&#12290;        </p></div><div class="sect1" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="queryhql-aggregation"></a>11.5.&nbsp;&#32479;&#35745;&#20989;&#25968;(Aggregate functions)</h2></div></div><div></div></div><p>            HQL&#26597;&#35810;&#21487;&#20197;&#36820;&#22238;&#23646;&#24615;&#30340;&#32479;&#35745;&#20989;&#25968;&#30340;&#32467;&#26524;&#12290;        </p><pre class="programlisting">select avg(cat.weight), sum(cat.weight), max(cat.weight), count(cat)from eg.Cat cat</pre><p>            &#22312;<tt class="literal">select</tt>&#23376;&#21477;&#20013;&#65292;&#32479;&#35745;&#20989;&#25968;&#30340;&#21464;&#37327;&#20063;&#21487;&#20197;&#26159;&#38598;&#21512;&#12290;        </p><pre class="programlisting">select cat, count( elements(cat.kittens) ) from eg.Cat cat group by cat</pre><p>            &#19979;&#38754;&#26159;&#25903;&#25345;&#30340;&#32479;&#35745;&#20989;&#25968;&#21015;&#34920;&#65306;        </p><div class="itemizedlist"><ul type="disc" compact><li><p>                    <tt class="literal">avg(...), sum(...), min(...), max(...)</tt>                </p></li><li><p>                    <tt class="literal">count(*)</tt>                </p></li><li><p>                    <tt class="literal">count(...), count(distinct ...), count(all...)</tt>                </p></li></ul></div><p>        <tt class="literal">distinct</tt> &#21644; <tt class="literal">all</tt>&#20851;&#38190;&#23383;&#30340;&#29992;&#27861;&#21644;&#35821;&#20041;&#19982;SQL&#30456;&#21516;&#12290;    </p><pre class="programlisting">select distinct cat.name from eg.Cat catselect count(distinct cat.name), count(cat) from eg.Cat cat</pre></div><div class="sect1" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="queryhql-polymorphism"></a>11.6.&nbsp;&#22810;&#24577;(polymorphism)&#26597;&#35810;</h2></div></div><div></div></div><p>            &#31867;&#20284;&#19979;&#38754;&#30340;&#26597;&#35810;&#65306;        </p><pre class="programlisting">from eg.Cat as cat</pre><p>        	&#36820;&#22238;&#30340;&#23454;&#20363;&#19981;&#20165;&#20165;&#26159;<tt class="literal">Cat</tt>&#65292;&#20063;&#26377;&#21487;&#33021;&#26159;&#23376;&#31867;&#30340;&#23454;&#20363;&#65292;&#27604;&#22914;<tt class="literal">DomesticCat</tt>&#12290;Hibernate&#26597;&#35810;&#21487;&#20197;&#22312;<tt class="literal">from</tt>&#23376;&#21477;&#20013;&#20351;&#29992;<span class="emphasis"><em>&#20219;&#20309;</em></span>Java&#31867;&#25110;&#32773;&#25509;&#21475;&#30340;&#21517;&#23383;&#12290;&#26597;&#35810;&#21487;&#33021;&#36820;&#22238;&#25152;&#26377;&#32487;&#25215;&#33258;&#36825;&#20010;&#31867;&#25110;&#32773;&#23454;&#29616;&#36825;&#20010;&#25509;&#21475;&#30340;&#25345;&#20037;&#21270;&#31867;&#30340;&#23454;&#20363;&#12290;&#19979;&#21015;&#26597;&#35810;&#20250;&#36820;&#22238;&#25152;&#26377;&#30340;&#25345;&#20037;&#21270;&#23545;&#35937;&#65306;        </p><pre class="programlisting">from java.lang.Object o</pre><p>            &#21487;&#33021;&#26377;&#22810;&#20010;&#25345;&#20037;&#21270;&#31867;&#37117;&#23454;&#29616;&#20102;<tt class="literal">Named</tt>&#25509;&#21475;&#65306;        </p><pre class="programlisting">from eg.Named n, eg.Named m where n.name = m.name</pre><p>

⌨️ 快捷键说明

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