Red Hat Training

A Red Hat training course is available for Red Hat JBoss Web Server

8.8. 式

where 句で許可される式には、SQL で記述できるほとんどのものが含まれます。
  • 算術演算子 +, -, *, /
  • バイナリ比較演算子 =, >=, <=, <>, !=, like
  • 論理演算子 and, or, not
  • かっこ ( ) (グルーピングを示す)
  • innot inbetweenis nullis not nullis emptyis not emptymember of、および not member of
  • "単純" 条件 case ... when ... then ... else ... end と "検索" 条件 case when ... then ... else ... end (HQL に固有)
  • 文字列連結 ...||... または concat(...,...) (use concat() for portable EJB-QL queries)
  • current_date()current_time()current_timestamp()
  • second(...)minute(...)hour(...)day(...)month(...)year(...) (HQL に固有)
  • EJB-QL 3.0 で定義された任意の関数または演算子: substring(), trim(), lower(), upper(), length(), locate(), abs(), sqrt(), bit_length()
  • coalesce() および nullif()
  • cast(... as ...) (2 つ目の引数は Hibernate タイプの名前) と extract(... from ...) (ANSI cast()extract() が基礎となるデータベースでサポートされている場合)
  • sign()trunc()rtrim()sin() などのデータベースでサポートされた任意の SQL スカラー関数
  • JDBC IN パラメータ ?
  • 名前付きパラメータ :name:start_date:x1
  • SQL リテラル 'foo'69'1970-01-01 10:00:01.0'
  • Java public static final 定数 eg.Color.TABBY
inbetween は以下のように使用できます。
select cat from DomesticCat cat where cat.name between 'A' and 'B'
select cat from DomesticCat cat where cat.name in ( 'Foo', 'Bar', 'Baz' )
否定形式は以下のように記述できます。
select cat from DomesticCat cat where cat.name not between 'A' and 'B'
select cat from DomesticCat cat where cat.name not in ( 'Foo', 'Bar', 'Baz' )
同様に、null 値のテストのために is nullis not null を使用できます。
Hibernate 設定で HQL クエリの置換を宣言することにより、ブール値を式で簡単に使用できます。
hibernate.query.substitutions true 1, false 0
これにより、この HQL から変換された SQL でキーワード truefalse がリテラル 10 に置換されます。
select cat from Cat cat where cat.alive = true
特別なプロパティ size または特別な size() 関数 (HQL 固有の機能) でコレクションのサイズをテストできます。
select cat from Cat cat where cat.kittens.size > 0
select cat from Cat cat where size(cat.kittens) > 0
インデックス化されたコレクションの場合は、minindex 関数と maxindex 関数を使用して最小インデックスと最大インデックスを参照できます。同様に、minelement 関数と maxelement 関数を使用して基本タイプのコレクションの最小エレメントと最大エレメントを参照できます。これらは HQL 固有の機能です。
select cal from Calendar cal where maxelement(cal.holidays) > current date
select order from Order order where maxindex(order.items) > 100
select order from Order order where minelement(order.items) > 10000
SQL 関数 any, some, all, exists, in は、コレクションのエレメントまたはインデックスセット (elements 関数および indices 関数) あるいはサブクエリの結果 (下記参照) が渡された場合にサポートされます。サブクエリは EJB-QL によりサポートされますが、elementsindices は固有の HQL の機能です。
select mother from Cat as mother, Cat as kit
where kit in elements(foo.kittens)
select p from NameList list, Person p
where p.name = some elements(list.names)
select cat from Cat cat where exists elements(cat.kittens)
select cat from Player p where 3 > all elements(p.scores)
select cat from Show show where 'fizard' in indices(show.acts)
コンストラクト sizeelementsindicesminindexmaxindexminelementmaxelement は Hibernate の where 句でのみ使用できます。
HQL では、インデックス化されたコレクション (アレイ、リスト、マップ) のエレメントはインデックスにより参照できます (where 句のみ)。
select order from Order order where order.items[0].id = 1234
select person from Person person, Calendar calendar
where calendar.holidays['national day'] = person.birthDay
    and person.nationality.calendar = calendar
select item from Item item, Order order
where order.items[ order.deliveredItemIndices[0] ] = item and order.id = 11
select item from Item item, Order order
where order.items[ maxindex(order.items) ] = item and order.id = 11
[] の内側の式は算術式にすることもできます。
select item from Item item, Order order
where order.items[ size(order.items) - 1 ] = item
また、1 対多の関係または値のコレクションのエレメントに対して HQL は組込みの index() 関数も提供します。
select item, index(item) from Order order 
    join order.items item
where index(item) < 5
基礎となるデータベースでサポートされたスカラー SQL 関数を使用できます。
select cat from DomesticCat cat where upper(cat.name) like 'FRI%'
HQL の利点をまだ理解できない場合は、以下の SQL クエリがどれだけ長く、読み難いか確認してください。
select cust
from Product prod,
    Store store
    inner join store.customers cust
where prod.name = 'widget'
    and store.location.name in ( 'Melbourne', 'Sydney' )
    and prod = all elements(cust.currentOrder.lineItems)
ヒント: 以下のように長く読みにくくなります。
SELECT cust.name, cust.address, cust.phone, cust.id, cust.current_order
FROM customers cust,
    stores store,
    locations loc,
    store_customers sc,
    product prod
WHERE 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
    )