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

📄 queryhql.html

📁 是一个中文的Hibernate库文档
💻 HTML
📖 第 1 页 / 共 4 页
字号:
<html><head>      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">   <title>&#31532;&nbsp;15&nbsp;&#31456;&nbsp;HQL: Hibernate&#26597;&#35810;&#35821;&#35328;</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="batch.html" title="&#31532;&nbsp;14&nbsp;&#31456;&nbsp;&#25209;&#37327;&#22788;&#29702;&#65288;Batch processing&#65289;"><link rel="next" href="querycriteria.html" title="&#31532;&nbsp;16&nbsp;&#31456;&nbsp;&#xA;    &#26465;&#20214;&#26597;&#35810;(Criteria Queries)&#xA;    "></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;15&nbsp;&#31456;&nbsp;HQL: Hibernate&#26597;&#35810;&#35821;&#35328;</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="batch.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;15&nbsp;&#31456;&nbsp;HQL: Hibernate&#26597;&#35810;&#35821;&#35328;</h2></div></div><div></div></div><p>        Hibernate&#37197;&#22791;&#20102;&#19968;&#31181;&#38750;&#24120;&#24378;&#22823;&#30340;&#26597;&#35810;&#35821;&#35328;&#65292;&#36825;&#31181;&#35821;&#35328;&#30475;&#19978;&#21435;&#24456;&#20687;SQL&#12290;&#20294;&#26159;&#19981;&#35201;&#34987;&#35821;&#27861;&#32467;&#26500;        &#19978;&#30340;&#30456;&#20284;&#25152;&#36855;&#24785;&#65292;HQL&#26159;&#38750;&#24120;&#26377;&#24847;&#35782;&#30340;&#34987;&#35774;&#35745;&#20026;&#23436;&#20840;&#38754;&#21521;&#23545;&#35937;&#30340;&#26597;&#35810;&#65292;&#23427;&#21487;&#20197;&#29702;&#35299;&#22914;&#32487;&#25215;&#12289;&#22810;&#24577;        &#21644;&#20851;&#32852;&#20043;&#31867;&#30340;&#27010;&#24565;&#12290;    </p><div class="sect1" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="queryhql-casesensitivity"></a>15.1.&nbsp;&#22823;&#23567;&#20889;&#25935;&#24863;&#24615;&#38382;&#39064;</h2></div></div><div></div></div><p>            &#38500;&#20102;Java&#31867;&#19982;&#23646;&#24615;&#30340;&#21517;&#31216;&#22806;&#65292;&#26597;&#35810;&#35821;&#21477;&#23545;&#22823;&#23567;&#20889;&#24182;&#19981;&#25935;&#24863;&#12290;            &#25152;&#20197; <tt class="literal">SeLeCT</tt> &#19982;            <tt class="literal">sELEct</tt> &#20197;&#21450;            <tt class="literal">SELECT</tt> &#26159;&#30456;&#21516;&#30340;&#65292;&#20294;&#26159;            <tt class="literal">org.hibernate.eg.FOO</tt> &#24182;&#19981;&#31561;&#20215;&#20110;            <tt class="literal">org.hibernate.eg.Foo</tt> &#24182;&#19988;            <tt class="literal">foo.barSet</tt> &#20063;&#19981;&#31561;&#20215;&#20110;            <tt class="literal">foo.BARSET</tt>&#12290;        </p><p>            &#26412;&#25163;&#20876;&#20013;&#30340;HQL&#20851;&#38190;&#23383;&#23558;&#20351;&#29992;&#23567;&#20889;&#23383;&#27597;. &#24456;&#22810;&#29992;&#25143;&#21457;&#29616;&#20351;&#29992;&#23436;&#20840;&#22823;&#20889;&#30340;&#20851;&#38190;&#23383;&#20250;&#20351;&#26597;&#35810;&#35821;&#21477;	     &#30340;&#21487;&#35835;&#24615;&#26356;&#24378;, &#20294;&#25105;&#20204;&#21457;&#29616;&#65292;&#24403;&#25226;&#26597;&#35810;&#35821;&#21477;&#23884;&#20837;&#21040;Java&#35821;&#21477;&#20013;&#30340;&#26102;&#20505;&#20351;&#29992;&#22823;&#20889;&#20851;&#38190;&#23383;&#27604;&#36739;&#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>15.2.&nbsp;from&#23376;&#21477;</h2></div></div><div></div></div><p>            Hibernate&#20013;&#26368;&#31616;&#21333;&#30340;&#26597;&#35810;&#35821;&#21477;&#30340;&#24418;&#24335;&#22914;&#19979;&#65306;        </p><pre class="programlisting">from eg.Cat</pre><p>            &#35813;&#23376;&#21477;&#31616;&#21333;&#30340;&#36820;&#22238;<tt class="literal">eg.Cat</tt>&#31867;&#30340;&#25152;&#26377;&#23454;&#20363;&#12290;            &#36890;&#24120;&#25105;&#20204;&#19981;&#38656;&#35201;&#20351;&#29992;&#31867;&#30340;&#20840;&#38480;&#23450;&#21517;, &#22240;&#20026; <tt class="literal">auto-import</tt>&#65288;&#33258;&#21160;&#24341;&#20837;&#65289;            &#26159;&#32570;&#30465;&#30340;&#24773;&#20917;&#12290; &#25152;&#20197;&#25105;&#20204;&#20960;&#20046;&#21482;&#20351;&#29992;&#22914;&#19979;&#30340;&#31616;&#21333;&#20889;&#27861;&#65306;        </p><pre class="programlisting">from Cat</pre><p>            &#22823;&#22810;&#25968;&#24773;&#20917;&#19979;, &#20320;&#38656;&#35201;&#25351;&#23450;&#19968;&#20010;<span class="emphasis"><em>&#21035;&#21517;</em></span>, &#21407;&#22240;&#26159;&#20320;&#21487;&#33021;&#38656;&#35201;            &#22312;&#26597;&#35810;&#35821;&#21477;&#30340;&#20854;&#23427;&#37096;&#20998;&#24341;&#29992;&#21040;<tt class="literal">Cat</tt>        </p><pre class="programlisting">from Cat as cat</pre><p>            &#36825;&#20010;&#35821;&#21477;&#25226;&#21035;&#21517;<tt class="literal">cat</tt>&#25351;&#23450;&#32473;&#31867;<tt class="literal">Cat</tt>            &#30340;&#23454;&#20363;, &#36825;&#26679;&#25105;&#20204;&#23601;&#21487;&#20197;&#22312;&#38543;&#21518;&#30340;&#26597;&#35810;&#20013;&#20351;&#29992;&#27492;&#21035;&#21517;&#20102;&#12290; &#20851;&#38190;&#23383;<tt class="literal">as</tt>            &#26159;&#21487;&#36873;&#30340;&#65292;&#25105;&#20204;&#20063;&#21487;&#20197;&#36825;&#26679;&#20889;:        </p><pre class="programlisting">from Cat cat</pre><p>            &#23376;&#21477;&#20013;&#21487;&#20197;&#21516;&#26102;&#20986;&#29616;&#22810;&#20010;&#31867;, &#20854;&#26597;&#35810;&#32467;&#26524;&#26159;&#20135;&#29983;&#19968;&#20010;&#31515;&#21345;&#20799;&#31215;&#25110;&#20135;&#29983;&#36328;&#34920;&#30340;&#36830;&#25509;&#12290;        </p><pre class="programlisting">from Formula, Parameter</pre><pre class="programlisting">from Formula as form, Parameter as param</pre><p>            &#26597;&#35810;&#35821;&#21477;&#20013;&#21035;&#21517;&#30340;&#24320;&#22836;&#37096;&#20998;&#23567;&#20889;&#34987;&#35748;&#20026;&#26159;&#23454;&#36341;&#20013;&#30340;&#22909;&#20064;&#24815;&#65292;            &#36825;&#26679;&#20570;&#19982;Java&#21464;&#37327;&#30340;&#21629;&#21517;&#26631;&#20934;&#20445;&#25345;&#20102;&#19968;&#33268;            (&#27604;&#22914;&#65292;<tt class="literal">domesticCat</tt>)&#12290;        </p></div><div class="sect1" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="queryhql-joins"></a>15.3.&nbsp;&#20851;&#32852;(Association)&#19982;&#36830;&#25509;(Join)</h2></div></div><div></div></div><p>            &#25105;&#20204;&#20063;&#21487;&#20197;&#20026;&#30456;&#20851;&#32852;&#30340;&#23454;&#20307;&#29978;&#33267;&#26159;&#23545;&#19968;&#20010;&#38598;&#21512;&#20013;&#30340;&#20840;&#37096;&#20803;&#32032;&#25351;&#23450;&#19968;&#20010;&#21035;&#21517;, &#36825;&#26102;&#35201;&#20351;&#29992;&#20851;&#38190;&#23383;<tt class="literal">join</tt>&#12290;        </p><pre class="programlisting">from Cat as cat     inner join cat.mate as mate    left outer join cat.kittens as kitten</pre><pre class="programlisting">from Cat as cat left join cat.mate.kittens as kittens</pre><pre class="programlisting">from Formula form full join form.parameter param</pre><p>            &#21463;&#25903;&#25345;&#30340;&#36830;&#25509;&#31867;&#22411;&#26159;&#20174;ANSI SQL&#20013;&#20511;&#37492;&#26469;&#30340;&#12290;        </p><div class="itemizedlist"><ul type="disc" compact><li><p>                    <tt class="literal">inner join</tt>&#65288;&#20869;&#36830;&#25509;&#65289;                </p></li><li><p>                    <tt class="literal">left outer join</tt>&#65288;&#24038;&#22806;&#36830;&#25509;&#65289;                </p></li><li><p>                    <tt class="literal">right outer join</tt>&#65288;&#21491;&#22806;&#36830;&#25509;&#65289;                </p></li><li><p>                    <tt class="literal">full join</tt> (&#20840;&#36830;&#25509;&#65292;&#24182;&#19981;&#24120;&#29992;)                </p></li></ul></div><p>            &#35821;&#21477;<tt class="literal">inner join</tt>, <tt class="literal">left outer join</tt> &#20197;&#21450;            <tt class="literal">right outer join</tt> &#21487;&#20197;&#31616;&#20889;&#12290;        </p><pre class="programlisting">from Cat as cat     join cat.mate as mate    left join cat.kittens as kitten</pre><p>            &#36824;&#26377;&#65292;&#19968;&#20010;"fetch"&#36830;&#25509;&#20801;&#35768;&#20165;&#20165;&#20351;&#29992;&#19968;&#20010;&#36873;&#25321;&#35821;&#21477;&#23601;&#23558;&#30456;&#20851;&#32852;&#30340;&#23545;&#35937;&#25110;&#19968;&#32452;&#20540;&#30340;&#38598;&#21512;&#38543;&#30528;&#20182;&#20204;&#30340;&#29238;&#23545;&#35937;&#30340;&#21021;&#22987;&#21270;&#32780;&#34987;&#21021;&#22987;&#21270;&#65292;&#36825;&#31181;&#26041;&#27861;&#22312;&#20351;&#29992;&#21040;&#38598;&#21512;&#30340;&#24773;&#20917;&#19979;&#23588;&#20854;&#26377;&#29992;&#65292;&#23545;&#20110;&#20851;&#32852;&#21644;&#38598;&#21512;&#26469;&#35828;&#65292;&#23427;&#26377;&#25928;&#30340;&#20195;&#26367;&#20102;&#26144;&#23556;&#25991;&#20214;&#20013;&#30340;&#22806;&#32852;&#25509;            &#19982;&#24310;&#36831;&#22768;&#26126;&#65288;lazy declarations&#65289;. &#26597;&#30475;            <a href="performance.html#performance-fetching" title="20.1.&nbsp;&#xA;	&#xA;			&#25235;&#21462;&#31574;&#30053;(Fetching strategies)&#xA;		">&#31532;&nbsp;20.1&nbsp;&#33410; &#8220;				&#25235;&#21462;&#31574;&#30053;(Fetching strategies)		&#8221;</a> &#20197;&#33719;&#24471;&#31561;&#22810;&#30340;&#20449;&#24687;&#12290;        </p><pre class="programlisting">from Cat as cat     inner join fetch cat.mate    left join fetch cat.kittens</pre><p>            &#19968;&#20010;fetch&#36830;&#25509;&#36890;&#24120;&#19981;&#38656;&#35201;&#34987;&#25351;&#23450;&#21035;&#21517;, &#22240;&#20026;&#30456;&#20851;&#32852;&#30340;&#23545;&#35937;&#19981;&#24212;&#24403;&#34987;&#29992;&#22312;            <tt class="literal">where</tt> &#23376;&#21477; (&#25110;&#20854;&#23427;&#20219;&#20309;&#23376;&#21477;)&#20013;&#12290;&#21516;&#26102;&#65292;&#30456;&#20851;&#32852;&#30340;&#23545;&#35937;            &#24182;&#19981;&#22312;&#26597;&#35810;&#30340;&#32467;&#26524;&#20013;&#30452;&#25509;&#36820;&#22238;&#65292;&#20294;&#21487;&#20197;&#36890;&#36807;&#20182;&#20204;&#30340;&#29238;&#23545;&#35937;&#26469;&#35775;&#38382;&#21040;&#20182;&#20204;&#12290;        </p><p>            &#27880;&#24847;<tt class="literal">fetch</tt>&#26500;&#36896;&#21464;&#37327;&#22312;&#20351;&#29992;&#20102;<tt class="literal">scroll()</tt> &#25110; <tt class="literal">iterate()</tt>&#20989;&#25968;            &#30340;&#26597;&#35810;&#20013;&#26159;&#19981;&#33021;&#20351;&#29992;&#30340;&#12290;&#26368;&#21518;&#27880;&#24847;&#65292;&#20351;&#29992;<tt class="literal">full join fetch</tt> &#19982; <tt class="literal">right join fetch</tt>&#26159;&#27809;&#26377;&#24847;&#20041;&#30340;&#12290;        </p><p>            &#22914;&#26524;&#20320;&#20351;&#29992;&#23646;&#24615;&#32423;&#21035;&#30340;&#24310;&#36831;&#33719;&#21462;&#65288;lazy fetching&#65289;&#65288;&#36825;&#26159;&#36890;&#36807;&#37325;&#26032;&#32534;&#20889;&#23383;&#33410;&#30721;&#23454;&#29616;&#30340;&#65289;&#65292;&#21487;&#20197;&#20351;&#29992; <tt class="literal">fetch  all properties</tt>             &#26469;&#24378;&#21046;Hibernate&#31435;&#21363;&#21462;&#24471;&#37027;&#20123;&#21407;&#26412;&#38656;&#35201;&#24310;&#36831;&#21152;&#36733;&#30340;&#23646;&#24615;&#65288;&#22312;&#31532;&#19968;&#20010;&#26597;&#35810;&#20013;&#65289;&#12290;        </p><pre class="programlisting">from Document fetch all properties order by name</pre><pre class="programlisting">from Document doc fetch all properties where lower(doc.name) like '%cats%'</pre></div><div class="sect1" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="queryhql-select"></a>15.4.&nbsp;select&#23376;&#21477;</h2></div></div><div></div></div><p>            <tt class="literal">select</tt> &#23376;&#21477;&#36873;&#25321;&#23558;&#21738;&#20123;&#23545;&#35937;&#19982;&#23646;&#24615;&#36820;            &#22238;&#21040;&#26597;&#35810;&#32467;&#26524;&#38598;&#20013;. &#32771;&#34385;&#22914;&#19979;&#24773;&#20917;:        </p><pre class="programlisting">select mate from Cat as cat     inner join cat.mate as mate</pre><p>            &#35813;&#35821;&#21477;&#23558;&#36873;&#25321;<tt class="literal">mate</tt>s of other <tt class="literal">Cat</tt>s&#12290;&#65288;&#20854;&#20182;&#29483;&#30340;&#37197;&#20598;&#65289;            &#23454;&#38469;&#19978;, &#20320;&#21487;&#20197;&#26356;&#31616;&#27905;&#30340;&#29992;&#20197;&#19979;&#30340;&#26597;&#35810;&#35821;&#21477;&#34920;&#36798;&#30456;&#21516;&#30340;&#21547;&#20041;:        </p><pre class="programlisting">select cat.mate from Cat cat</pre><p>            &#26597;&#35810;&#35821;&#21477;&#21487;&#20197;&#36820;&#22238;&#20540;&#20026;&#20219;&#20309;&#31867;&#22411;&#30340;&#23646;&#24615;&#65292;&#21253;&#25324;&#36820;&#22238;&#31867;&#22411;&#20026;&#26576;&#31181;&#32452;&#20214;(Component)&#30340;&#23646;&#24615;:        </p><pre class="programlisting">select cat.name from DomesticCat catwhere cat.name like 'fri%'</pre><pre class="programlisting">select cust.name.firstName from Customer as cust</pre><p>            &#26597;&#35810;&#35821;&#21477;&#21487;&#20197;&#36820;&#22238;&#22810;&#20010;&#23545;&#35937;&#21644;&#65288;&#25110;&#65289;&#23646;&#24615;&#65292;&#23384;&#25918;&#22312;            <tt class="literal">Object[]</tt>&#38431;&#21015;&#20013;,        </p><pre class="programlisting">select mother, offspr, mate.name from DomesticCat as mother    inner join mother.mate as mate    left outer join mother.kittens as offspr</pre><p>            &#25110;&#23384;&#25918;&#22312;&#19968;&#20010;<tt class="literal">List</tt>&#23545;&#35937;&#20013;,        </p><pre class="programlisting">select new list(mother, offspr, mate.name)from DomesticCat as mother    inner join mother.mate as mate    left outer join mother.kittens as offspr</pre><p>            &#20063;&#21487;&#33021;&#30452;&#25509;&#36820;&#22238;&#19968;&#20010;&#23454;&#38469;&#30340;&#31867;&#22411;&#23433;&#20840;&#30340;Java&#23545;&#35937;,        </p><pre class="programlisting">select new Family(mother, mate, offspr)from DomesticCat as mother    join mother.mate as mate    left join mother.kittens as offspr</pre><p>            &#20551;&#35774;&#31867;<tt class="literal">Family</tt>&#26377;&#19968;&#20010;&#21512;&#36866;&#30340;&#26500;&#36896;&#20989;&#25968;.        </p><p>            &#20320;&#21487;&#20197;&#20351;&#29992;&#20851;&#38190;&#23383;<tt class="literal">as</tt>&#32473;&#8220;&#34987;&#36873;&#25321;&#20102;&#30340;&#34920;&#36798;&#24335;&#8221;&#25351;&#27966;&#21035;&#21517;:        </p><pre class="programlisting">select max(bodyWeight) as max, min(bodyWeight) as min, count(*) as nfrom Cat cat</pre><p>            &#36825;&#31181;&#20570;&#27861;&#22312;&#19982;&#23376;&#21477;<tt class="literal">select new map</tt>&#19968;&#36215;&#20351;&#29992;&#26102;&#26368;&#26377;&#29992;:        </p><pre class="programlisting">select new map( max(bodyWeight) as max, min(bodyWeight) as min, count(*) as n )from Cat cat</pre><p>            &#35813;&#26597;&#35810;&#36820;&#22238;&#20102;&#19968;&#20010;<tt class="literal">Map</tt>&#30340;&#23545;&#35937;&#65292;&#20869;&#23481;&#26159;&#21035;&#21517;&#19982;&#34987;&#36873;&#25321;&#30340;&#20540;&#32452;&#25104;&#30340;&#21517;-&#20540;&#26144;&#23556;&#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>15.5.&nbsp;&#32858;&#38598;&#20989;&#25968;</h2></div></div><div></div></div><p>            HQL&#26597;&#35810;&#29978;&#33267;&#21487;&#20197;&#36820;&#22238;&#20316;&#29992;&#20110;&#23646;&#24615;&#20043;&#19978;&#30340;&#32858;&#38598;&#20989;&#25968;&#30340;&#35745;&#31639;&#32467;&#26524;:        </p><pre class="programlisting">select avg(cat.weight), sum(cat.weight), max(cat.weight), count(cat)from Cat cat</pre><p>            &#21463;&#25903;&#25345;&#30340;&#32858;&#38598;&#20989;&#25968;&#22914;&#19979;&#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>            &#20320;&#21487;&#20197;&#22312;&#36873;&#25321;&#23376;&#21477;&#20013;&#20351;&#29992;&#25968;&#23398;&#25805;&#20316;&#31526;&#12289;&#36830;&#25509;&#20197;&#21450;&#32463;&#36807;&#39564;&#35777;&#30340;SQL&#20989;&#25968;&#65306;

⌨️ 快捷键说明

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