📄 queryhql.html
字号:
from eg.Player p where 3 > all elements(p.scores)from eg.Show show where 'fizard' in indices(show.acts)</pre><p> 请注意这些设施:<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> 都有一些使用限制: </p><div class="itemizedlist"><ul type="disc" compact><li><p> 在<tt class="literal">where</tt>子句中: 只对支持子查询的数据库有效 </p></li><li><p> 在<tt class="literal">select</tt>子句中:只有<tt class="literal">elements</tt>和<tt class="literal">indices</tt>有效 </p></li></ul></div><p> 有序的集合(数组、list、map)的元素可以用索引来进行引用(只限于在where子句中) </p><pre class="programlisting">from Order order where order.items[0].id = 1234select person from Person person, Calendar calendarwhere calendar.holidays['national day'] = person.birthDay and person.nationality.calendar = calendarselect item from Item item, Order orderwhere order.items[ order.deliveredItemIndices[0] ] = item and order.id = 11select item from Item item, Order orderwhere order.items[ maxindex(order.items) ] = item and order.id = 11</pre><p> <tt class="literal">[]</tt>中的表达式允许是另一个数学表达式。 </p><pre class="programlisting">select item from Item item, Order orderwhere order.items[ size(order.items) - 1 ] = item</pre><p> HQL也对一对多关联或者值集合提供内置的<tt class="literal">index()</tt>函数。 </p><pre class="programlisting">select item, index(item) from Order order join order.items itemwhere index(item) < 5</pre><p> 底层数据库支持的标量SQL函数也可以使用 </p><pre class="programlisting">from eg.DomesticCat cat where upper(cat.name) like 'FRI%'</pre><p> 假如以上的这些还没有让你信服的话,请想象一下下面的查询假若用SQL来写,会变得多么长,多么不可读: </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>提示:</em></span>对应的SQL语句可能是这样的 </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>11.9. order by 子句</h2></div></div><div></div></div><p> 查询返回的列表可以按照任何返回的类或者组件的属性排序: </p><pre class="programlisting">from eg.DomesticCat catorder by cat.name asc, cat.weight desc, cat.birthdate</pre><p> <tt class="literal">asc</tt>和<tt class="literal">desc</tt>是可选的,分别代表升序或者降序。 </p></div><div class="sect1" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="queryhql-grouping"></a>11.10. group by 子句</h2></div></div><div></div></div><p> 返回统计值的查询可以按照返回的类或者组件的任何属性排序: </p><pre class="programlisting">select cat.color, sum(cat.weight), count(cat) from eg.Cat catgroup by cat.colorselect foo.id, avg( elements(foo.names) ), max( indices(foo.names) ) from eg.Foo foogroup by foo.id</pre><p> 请注意:你可以在select子句中使用<tt class="literal">elements</tt>和<tt class="literal">indices</tt>指令,即使你的数据库不支持子查询也可以。 </p><p> <tt class="literal">having</tt>子句也是允许的。 </p><pre class="programlisting">select cat.color, sum(cat.weight), count(cat) from eg.Cat catgroup by cat.color having cat.color in (eg.Color.TABBY, eg.Color.BLACK)</pre><p> 在<tt class="literal">having</tt>子句中允许出现SQL函数和统计函数,当然这需要底层数据库支持才行。(比如说,MySQL就不支持) </p><pre class="programlisting">select catfrom eg.Cat cat join cat.kittens kittengroup by cathaving avg(kitten.weight) > 100order by count(kitten) asc, sum(kitten.weight) desc</pre><p> 注意,<tt class="literal">group by</tt>子句和<tt class="literal">order by</tt>子句都不支持数学表达式。 </p></div><div class="sect1" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="queryhql-subqueries"></a>11.11. 子查询</h2></div></div><div></div></div><p> 对于支持子查询的数据库来说,Hibernate支持在查询中嵌套子查询。子查询必须由圆括号包围(常常是在一个SQL统计函数中)。也允许关联子查询(在外部查询中作为一个别名出现的子查询)。 </p><pre class="programlisting">from eg.Cat as fatcat where fatcat.weight > ( select avg(cat.weight) from eg.DomesticCat cat )from eg.DomesticCat as cat where cat.name = some ( select name.nickName from eg.Name as name ) from eg.Cat as cat where not exists ( from eg.Cat as mate where mate.mate = cat )from eg.DomesticCat as cat where cat.name not in ( select name.nickName from eg.Name as name )</pre></div><div class="sect1" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="queryhql-examples"></a>11.12. HQL示例</h2></div></div><div></div></div><p> Hibernate查询可以非常强大复杂。实际上,强有力的查询语言是Hibernate的主要卖点之一。下面给出的示例与我在近期实际项目中使用的一些查询很类似。请注意你编写的查询大部分等都不会这么复杂! </p><p> 下面的查询对特定的客户,根据给定的最小总计值(minAmount),查询出所有未付订单,返回其订单号、货品总数、订单总金额,结果按照总金额排序。在决定价格的时候,参考当前目录。产生的SQL查询,在<tt class="literal">ORDER</tt>,<tt class="literal">ORDER_LINE</tt>,<tt class="literal">PRODUCT</tt>,<tt class="literal">CATALOG</tt>和<tt class="literal">PRICE</tt>表之间有四个内部连接和一个没有产生关联的字查询。
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -