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

📄 queryhql.html

📁 是一个中文的Hibernate库文档
💻 HTML
📖 第 1 页 / 共 4 页
字号:
            &#32780;&#19988;&#21542;&#23450;&#30340;&#26684;&#24335;&#20063;&#21487;&#20197;&#22914;&#19979;&#20070;&#20889;&#65306;        </p><pre class="programlisting">from DomesticCat cat where cat.name not between 'A' and 'B'</pre><pre class="programlisting">from DomesticCat cat where cat.name not in ( 'Foo', 'Bar', 'Baz' )</pre><p>            &#21516;&#26679;, &#23376;&#21477;<tt class="literal">is null</tt>&#19982;<tt class="literal">is not null</tt>&#21487;&#20197;&#34987;&#29992;&#26469;&#27979;&#35797;&#31354;&#20540;(null).        </p><p>            &#22312;Hibernate&#37197;&#32622;&#25991;&#20214;&#20013;&#22768;&#26126;HQL&#8220;&#26597;&#35810;&#26367;&#20195;&#65288;query substitutions&#65289;&#8221;&#20043;&#21518;&#65292;            &#24067;&#23572;&#34920;&#36798;&#24335;&#65288;Booleans&#65289;&#21487;&#20197;&#22312;&#20854;&#20182;&#34920;&#36798;&#24335;&#20013;&#36731;&#26494;&#30340;&#20351;&#29992;:        </p><pre class="programlisting">&lt;property name="hibernate.query.substitutions"&gt;true 1, false 0&lt;/property&gt;</pre><p>            &#31995;&#32479;&#23558;&#35813;HQL&#36716;&#25442;&#20026;SQL&#35821;&#21477;&#26102;&#65292;&#35813;&#35774;&#32622;&#34920;&#26126;&#23558;&#29992;&#23383;&#31526; <tt class="literal">1</tt> &#21644;  <tt class="literal">0</tt> &#26469;            &#21462;&#20195;&#20851;&#38190;&#23383;<tt class="literal">true</tt> &#21644; <tt class="literal">false</tt>:        </p><pre class="programlisting">from Cat cat where cat.alive = true</pre><p>            &#20320;&#21487;&#20197;&#29992;&#29305;&#27530;&#23646;&#24615;<tt class="literal">size</tt>, &#25110;&#26159;&#29305;&#27530;&#20989;&#25968;<tt class="literal">size()</tt>&#27979;&#35797;&#19968;&#20010;&#38598;&#21512;&#30340;&#22823;&#23567;&#12290;        </p><pre class="programlisting">from Cat cat where cat.kittens.size &gt; 0</pre><pre class="programlisting">from Cat cat where size(cat.kittens) &gt; 0</pre><p>            &#23545;&#20110;&#32034;&#24341;&#20102;&#65288;&#26377;&#24207;&#65289;&#30340;&#38598;&#21512;&#65292;&#20320;&#21487;&#20197;&#20351;&#29992;<tt class="literal">minindex</tt> &#19982; <tt class="literal">maxindex</tt>&#20989;&#25968;&#26469;&#24341;&#29992;&#21040;&#26368;&#23567;&#19982;&#26368;&#22823;&#30340;&#32034;&#24341;&#24207;&#25968;&#12290;            &#21516;&#29702;&#65292;&#20320;&#21487;&#20197;&#20351;&#29992;<tt class="literal">minelement</tt> &#19982; <tt class="literal">maxelement</tt>&#20989;&#25968;&#26469; &#24341;&#29992;&#21040;&#19968;&#20010;&#22522;&#26412;&#25968;&#25454;&#31867;&#22411;&#30340;&#38598;&#21512;&#20013;&#26368;&#23567;&#19982;&#26368;&#22823;&#30340;&#20803;&#32032;&#12290;        </p><pre class="programlisting">from Calendar cal where maxelement(cal.holidays) &gt; current date</pre><pre class="programlisting">from Order order where maxindex(order.items) &gt; 100</pre><pre class="programlisting">from Order order where minelement(order.items) &gt; 10000</pre><p>&#22312;&#20256;&#36882;&#19968;&#20010;&#38598;&#21512;&#30340;&#32034;&#24341;&#38598;&#25110;&#32773;&#26159;&#20803;&#32032;&#38598;(<tt class="literal">elements</tt>&#19982;<tt class="literal">indices</tt> &#20989;&#25968;)            &#25110;&#32773;&#20256;&#36882;&#19968;&#20010;&#23376;&#26597;&#35810;&#30340;&#32467;&#26524;&#30340;&#26102;&#20505;&#65292;&#21487;&#20197;&#20351;&#29992;SQL&#20989;&#25968;<tt class="literal">any, some, all, exists, in</tt>        </p><pre class="programlisting">select mother from Cat as mother, Cat as kitwhere kit in elements(foo.kittens)</pre><pre class="programlisting">select p from NameList list, Person pwhere p.name = some elements(list.names)</pre><pre class="programlisting">from Cat cat where exists elements(cat.kittens)</pre><pre class="programlisting">from Player p where 3 &gt; all elements(p.scores)</pre><pre class="programlisting">from Show show where 'fizard' in indices(show.acts)</pre><p>            &#27880;&#24847;&#65292;&#22312;Hibernate3&#31181;&#65292;&#36825;&#20123;&#32467;&#26500;&#21464;&#37327;- <tt class="literal">size</tt>, <tt class="literal">elements</tt>,            <tt class="literal">indices</tt>, <tt class="literal">minindex</tt>, <tt class="literal">maxindex</tt>,            <tt class="literal">minelement</tt>, <tt class="literal">maxelement</tt> - &#21482;&#33021;&#22312;where&#23376;&#21477;&#20013;&#20351;&#29992;&#12290;        </p><p>            &#19968;&#20010;&#34987;&#32034;&#24341;&#36807;&#30340;&#65288;&#26377;&#24207;&#30340;&#65289;&#38598;&#21512;&#30340;&#20803;&#32032;(arrays, lists, maps)&#21487;&#20197;&#22312;&#20854;&#20182;&#32034;&#24341;&#20013;&#34987;&#24341;&#29992;&#65288;&#21482;&#33021;&#22312;where&#23376;&#21477;&#20013;&#65289;&#65306;        </p><pre class="programlisting">from Order order where order.items[0].id = 1234</pre><pre class="programlisting">select person from Person person, Calendar calendarwhere calendar.holidays['national day'] = person.birthDay    and person.nationality.calendar = calendar</pre><pre class="programlisting">select item from Item item, Order orderwhere order.items[ order.deliveredItemIndices[0] ] = item and order.id = 11</pre><pre class="programlisting">select item from Item item, Order orderwhere order.items[ maxindex(order.items) ] = item and order.id = 11</pre><p>            &#22312;<tt class="literal">[]</tt>&#20013;&#30340;&#34920;&#36798;&#24335;&#29978;&#33267;&#21487;&#20197;&#26159;&#19968;&#20010;&#31639;&#25968;&#34920;&#36798;&#24335;&#12290;        </p><pre class="programlisting">select item from Item item, Order orderwhere order.items[ size(order.items) - 1 ] = item</pre><p>            &#23545;&#20110;&#19968;&#20010;&#19968;&#23545;&#22810;&#30340;&#20851;&#32852;&#65288;one-to-many association&#65289;&#25110;&#26159;&#20540;&#30340;&#38598;&#21512;&#20013;&#30340;&#20803;&#32032;&#65292;            HQL&#20063;&#25552;&#20379;&#20869;&#24314;&#30340;<tt class="literal">index()</tt>&#20989;&#25968;&#65292;        </p><pre class="programlisting">select item, index(item) from Order order     join order.items itemwhere index(item) &lt; 5</pre><p>            &#22914;&#26524;&#24213;&#23618;&#25968;&#25454;&#24211;&#25903;&#25345;&#26631;&#37327;&#30340;SQL&#20989;&#25968;&#65292;&#23427;&#20204;&#20063;&#21487;&#20197;&#34987;&#20351;&#29992;        </p><pre class="programlisting">from DomesticCat cat where upper(cat.name) like 'FRI%'</pre><p>            &#22914;&#26524;&#20320;&#36824;&#19981;&#33021;&#23545;&#25152;&#26377;&#30340;&#36825;&#20123;&#28145;&#20449;&#19981;&#30097;&#65292;&#24819;&#24819;&#19979;&#38754;&#30340;&#26597;&#35810;&#12290;&#22914;&#26524;&#20351;&#29992;SQL&#65292;&#35821;&#21477;&#38271;&#24230;&#20250;&#22686;&#38271;&#22810;&#23569;&#65292;&#21487;&#35835;&#24615;&#20250;&#19979;&#38477;&#22810;&#23569;&#65306;        </p><pre class="programlisting">select custfrom Product prod,    Store store    inner join store.customers custwhere prod.name = 'widget'    and store.location.name in ( 'Melbourne', 'Sydney' )    and prod = all elements(cust.currentOrder.lineItems)</pre><p>            <span class="emphasis"><em>&#25552;&#31034;:</em></span> &#20250;&#20687;&#22914;&#19979;&#30340;&#35821;&#21477;        </p><pre class="programlisting">SELECT cust.name, cust.address, cust.phone, cust.id, cust.current_orderFROM customers cust,    stores store,    locations loc,    store_customers sc,    product prodWHERE prod.name = 'widget'    AND store.loc_id = loc.id    AND loc.name IN ( 'Melbourne', 'Sydney' )    AND sc.store_id = store.id    AND sc.cust_id = cust.id    AND prod.id = ALL(        SELECT item.prod_id        FROM line_items item, orders o        WHERE item.order_id = o.id            AND cust.current_order = o.id    )</pre></div><div class="sect1" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="queryhql-ordering"></a>15.9.&nbsp;order by&#23376;&#21477;</h2></div></div><div></div></div><p>            &#26597;&#35810;&#36820;&#22238;&#30340;&#21015;&#34920;(list)&#21487;&#20197;&#25353;&#29031;&#19968;&#20010;&#36820;&#22238;&#30340;&#31867;&#25110;&#32452;&#20214;&#65288;components)&#20013;&#30340;&#20219;&#20309;&#23646;&#24615;&#65288;property&#65289;&#36827;&#34892;&#25490;&#24207;&#65306;        </p><pre class="programlisting">from DomesticCat catorder by cat.name asc, cat.weight desc, cat.birthdate</pre><p>            &#21487;&#36873;&#30340;<tt class="literal">asc</tt>&#25110;<tt class="literal">desc</tt>&#20851;&#38190;&#23383;&#25351;&#26126;&#20102;&#25353;&#29031;&#21319;&#24207;&#25110;&#38477;&#24207;&#36827;&#34892;&#25490;&#24207;.        </p></div><div class="sect1" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="queryhql-grouping"></a>15.10.&nbsp;group by&#23376;&#21477;</h2></div></div><div></div></div><p>            &#19968;&#20010;&#36820;&#22238;&#32858;&#38598;&#20540;(aggregate values)&#30340;&#26597;&#35810;&#21487;&#20197;&#25353;&#29031;&#19968;&#20010;&#36820;&#22238;&#30340;&#31867;&#25110;&#32452;&#20214;&#65288;components)&#20013;&#30340;&#20219;&#20309;&#23646;&#24615;&#65288;property&#65289;&#36827;&#34892;&#20998;&#32452;&#65306;        </p><pre class="programlisting">select cat.color, sum(cat.weight), count(cat) from Cat catgroup by cat.color</pre><pre class="programlisting">select foo.id, avg(name), max(name) from Foo foo join foo.names namegroup by foo.id</pre><p>            <tt class="literal">having</tt>&#23376;&#21477;&#22312;&#36825;&#37324;&#20063;&#20801;&#35768;&#20351;&#29992;.        </p><pre class="programlisting">select cat.color, sum(cat.weight), count(cat) from Cat catgroup by cat.color having cat.color in (eg.Color.TABBY, eg.Color.BLACK)</pre><p>            &#22914;&#26524;&#24213;&#23618;&#30340;&#25968;&#25454;&#24211;&#25903;&#25345;&#30340;&#35805;(&#20363;&#22914;&#19981;&#33021;&#22312;MySQL&#20013;&#20351;&#29992;)&#65292;SQL&#30340;&#19968;&#33324;&#20989;&#25968;&#19982;&#32858;&#38598;&#20989;&#25968;&#20063;&#21487;&#20197;&#20986;&#29616;            &#22312;<tt class="literal">having</tt>&#19982;<tt class="literal">order by</tt> &#23376;&#21477;&#20013;&#12290;            </p><pre class="programlisting">select catfrom Cat cat    join cat.kittens kittengroup by cathaving avg(kitten.weight) &gt; 100order by count(kitten) asc, sum(kitten.weight) desc</pre><p>            &#27880;&#24847;<tt class="literal">group by</tt>&#23376;&#21477;&#19982;            <tt class="literal">order by</tt>&#23376;&#21477;&#20013;&#37117;&#19981;&#33021;&#21253;&#21547;&#31639;&#26415;&#34920;&#36798;&#24335;&#65288;arithmetic expressions&#65289;.        </p></div><div class="sect1" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="queryhql-subqueries"></a>15.11.&nbsp;&#23376;&#26597;&#35810;</h2></div></div><div></div></div><p>            &#23545;&#20110;&#25903;&#25345;&#23376;&#26597;&#35810;&#30340;&#25968;&#25454;&#24211;&#65292;Hibernate&#25903;&#25345;&#22312;&#26597;&#35810;&#20013;&#20351;&#29992;&#23376;&#26597;&#35810;&#12290;&#19968;&#20010;&#23376;&#26597;&#35810;&#24517;&#39035;&#34987;&#22278;&#25324;&#21495;&#21253;&#22260;&#36215;&#26469;&#65288;&#32463;&#24120;&#26159;SQL&#32858;&#38598;&#20989;&#25968;&#30340;&#22278;&#25324;&#21495;&#65289;&#12290;            &#29978;&#33267;&#30456;&#20114;&#20851;&#32852;&#30340;&#23376;&#26597;&#35810;&#65288;&#24341;&#29992;&#21040;&#22806;&#37096;&#26597;&#35810;&#20013;&#30340;&#21035;&#21517;&#30340;&#23376;&#26597;&#35810;&#65289;&#20063;&#26159;&#20801;&#35768;&#30340;&#12290;        </p><pre class="programlisting">from Cat as fatcat where fatcat.weight &gt; (     select avg(cat.weight) from DomesticCat cat )</pre><pre class="programlisting">from DomesticCat as cat where cat.name = some (     select name.nickName from Name as name )</pre><pre class="programlisting">from Cat as cat where not exists (     from Cat as mate where mate.mate = cat )</pre><pre class="programlisting">from DomesticCat as cat where cat.name not in (     select name.nickName from Name as name )</pre><p>            &#22312;select&#21015;&#34920;&#20013;&#21253;&#21547;&#19968;&#20010;&#34920;&#36798;&#24335;&#20197;&#19978;&#30340;&#23376;&#26597;&#35810;&#65292;&#20320;&#21487;&#20197;&#20351;&#29992;&#19968;&#20010;&#20803;&#32452;&#26500;&#36896;&#31526;&#65288;tuple constructors&#65289;&#65306;         </p><pre class="programlisting">from Cat as cat where not ( cat.name, cat.color ) in (     select cat.name, cat.color from DomesticCat cat )</pre><p>            &#27880;&#24847;&#22312;&#26576;&#20123;&#25968;&#25454;&#24211;&#20013;&#65288;&#19981;&#21253;&#25324;Oracle&#19982;HSQL&#65289;&#65292;&#20320;&#20063;&#21487;&#20197;&#22312;&#20854;&#20182;&#35821;&#22659;&#20013;&#20351;&#29992;&#20803;&#32452;&#26500;&#36896;&#31526;&#65292;            &#27604;&#22914;&#26597;&#35810;&#29992;&#25143;&#31867;&#22411;&#30340;&#32452;&#20214;&#19982;&#32452;&#21512;&#65306;        </p><pre class="programlisting">from Person where name = ('Gavin', 'A', 'King')</pre><p>            &#35813;&#26597;&#35810;&#31561;&#20215;&#20110;&#26356;&#22797;&#26434;&#30340;&#65306;        </p><pre class="programlisting">from Person where name.first = 'Gavin' and name.initial = 'A' and name.last = 'King')</pre><p>            &#26377;&#20004;&#20010;&#24456;&#22909;&#30340;&#29702;&#30001;&#20351;&#20320;&#19981;&#24212;&#24403;&#20316;&#36825;&#26679;&#30340;&#20107;&#24773;&#65306;&#39318;&#20808;&#65292;&#23427;&#19981;&#23436;&#20840;&#36866;&#29992;&#20110;&#21508;&#20010;&#25968;&#25454;&#24211;&#24179;&#21488;&#65307;&#20854;&#27425;&#65292;&#26597;&#35810;&#29616;&#22312;&#20381;&#36182;&#20110;&#26144;&#23556;&#25991;&#20214;&#20013;&#23646;&#24615;&#30340;&#39034;&#24207;&#12290;        </p></div><div class="sect1" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="queryhql-examples"></a>15.12.&nbsp;HQL&#31034;&#20363;</h2></div></div><div></div></div><p>            Hibernate&#26597;&#35810;&#21487;&#20197;&#38750;&#24120;&#30340;&#24378;&#22823;&#19982;&#22797;&#26434;&#12290;&#23454;&#38469;&#19978;&#65292;Hibernate&#30340;&#19968;&#20010;&#20027;&#35201;&#21334;&#28857;&#23601;&#26159;&#26597;&#35810;&#35821;&#21477;&#30340;&#23041;&#21147;&#12290;&#36825;&#37324;&#26377;&#19968;&#20123;&#20363;&#23376;&#65292;&#23427;&#20204;&#19982;&#25105;&#22312;&#26368;&#36817;&#30340;            &#19968;&#20010;&#39033;&#30446;&#20013;&#20351;&#29992;&#30340;&#26597;&#35810;&#38750;&#24120;&#30456;&#20284;&#12290;&#27880;&#24847;&#20320;&#33021;&#29992;&#21040;&#30340;&#22823;&#22810;&#25968;&#26597;&#35810;&#27604;&#36825;&#20123;&#35201;&#31616;&#21333;&#30340;&#22810;&#65281;

⌨️ 快捷键说明

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