14.10. Expressions 式


where 節で使用する表現には以下が含まれます:
  • 算術演算子:+, -, *, /
  • 2項比較演算子:=, >=, <=, <>, !=, like
  • 論理演算子 and, or, not
  • グループ分けを表す括弧 ( )
  • in, not in, between, is null, is not null, is empty, is not empty, member of and not member of
  • "シンプル"な case case ... when ... then ... else ... end、 "探索的"な case case when ... then ... else ... end
  • ストリングの連結 ...||... または concat(...,...)
  • current_date()current_time()current_timestamp()
  • second(...)minute(...)hour(...)day(...)month(...)year(...)
  • EJB-QL 3.0 で定義されている関数や演算子: substring(), trim(), lower(), upper(), length(), locate(), abs(), sqrt(), bit_length(), mod()
  • coalesce()nullif()
  • 数字や時間の値を String にコンバートする str()
  • 2番目の引数が Hibernate 型の名前である cast(... as ...)extract(... from ...)。ただし使用するデータベースが ANSI cast()extract() をサポートする場合に限ります。
  • 結合したインデックス付きのコレクションの別名に適用される HQL の index() 関数。
  • コレクション値のパス表現を取る HQL 関数:size(), minelement(), maxelement(), minindex(), maxindex()some, all, exists, any, in を使って修飾することができる特別な elements()indices 関数と一緒に使います。
  • sign()trunc()rtrim()sin() など、データベース対応のSQL スカラ関数。
  • JDBC スタイルの位置パラメータ ?
  • 名前付きパラメータ: :name, :start_date, :x1
  • SQL リテラル: 'foo'696.66E+2'1970-01-01 10:00:01.0'
  • Java の public static final 定数: eg.Color.TABBY
inbetween は以下のように使用できます:
from DomesticCat cat where cat.name between 'A' and 'B'
Copy to Clipboard Toggle word wrap
from DomesticCat cat where cat.name in ( 'Foo', 'Bar', 'Baz' )
Copy to Clipboard Toggle word wrap
また、否定形で以下のように記述することもできます。
from DomesticCat cat where cat.name not between 'A' and 'B'
Copy to Clipboard Toggle word wrap
from DomesticCat cat where cat.name not in ( 'Foo', 'Bar', 'Baz' )
Copy to Clipboard Toggle word wrap
同様に is nullis not null は null 値をテストするために使用できます。
Hibernate 設定で HQL query substitutions を宣言すれば、boolean 値を式の中で簡単に使用できます:
<property name="hibernate.query.substitutions">true 1, false 0</property>
Copy to Clipboard Toggle word wrap
こうすることで下記の HQL を SQL に変換するときに truefalse キーワードは 10 に置き換えられます:
from Cat cat where cat.alive = true
Copy to Clipboard Toggle word wrap
特別なプロパティ size、または特別な関数 size() を使ってコレクションのサイズをテストできます。
from Cat cat where cat.kittens.size > 0
Copy to Clipboard Toggle word wrap
from Cat cat where size(cat.kittens) > 0
Copy to Clipboard Toggle word wrap
インデックス付きのコレクションでは、minindexmaxindex 関数を使って、インデックスの最小値と最大値を参照できます。同様に、 minelementmaxelement 関数を使って、基本型のコレクションの最小要素と最大要素を参照できます。
from Calendar cal where maxelement(cal.holidays) > current_date
Copy to Clipboard Toggle word wrap
from Order order where maxindex(order.items) > 100
Copy to Clipboard Toggle word wrap
from Order order where minelement(order.items) > 10000
Copy to Clipboard Toggle word wrap
コレクションの要素やインデックスのセット(elementsindices 関数)、または副問い合わせの結果が受け取れるときは、SQL 関数 any, some, all, exists, in がサポートされます(以下参照)。
select mother from Cat as mother, Cat as kit
where kit in elements(foo.kittens)
Copy to Clipboard Toggle word wrap
select p from NameList list, Person p
where p.name = some elements(list.names)
Copy to Clipboard Toggle word wrap
from Cat cat where exists elements(cat.kittens)
Copy to Clipboard Toggle word wrap
from Player p where 3 > all elements(p.scores)
Copy to Clipboard Toggle word wrap
from Show show where 'fizard' in indices(show.acts)
Copy to Clipboard Toggle word wrap
sizeelementsindicesminindexmaxindexminelementmaxelement は Hibernate3 の where 節だけで利用可能であることに注意してください。
インデックス付きのコレクション(arrays, lists, maps)の要素は、where節内でのみインデックスで参照できます:
from Order order where order.items[0].id = 1234
Copy to Clipboard Toggle word wrap
select person from Person person, Calendar calendar
where calendar.holidays['national day'] = person.birthDay
    and person.nationality.calendar = calendar
Copy to Clipboard Toggle word wrap
select item from Item item, Order order
where order.items[ order.deliveredItemIndices[0] ] = item and order.id = 11
Copy to Clipboard Toggle word wrap
select item from Item item, Order order
where order.items[ maxindex(order.items) ] = item and order.id = 11
Copy to Clipboard Toggle word wrap
[] 内部の式は、算術式の場合もあります。
select item from Item item, Order order
where order.items[ size(order.items) - 1 ] = item
Copy to Clipboard Toggle word wrap
HQL は一対多関連や値のコレクションの要素に対して、組み込みの index() 関数も用意しています。
select item, index(item) from Order order
    join order.items item
where index(item) < 5
Copy to Clipboard Toggle word wrap
ベースとなるデータベースがサポートしているスカラ SQL 関数が使用できます:
from DomesticCat cat where upper(cat.name) like 'FRI%'
Copy to Clipboard Toggle word wrap
下のクエリが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)
Copy to Clipboard Toggle word wrap
ヒント: 例えばこのように出来ます。
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
    )
Copy to Clipboard Toggle word wrap
トップに戻る
Red Hat logoGithubredditYoutubeTwitter

詳細情報

試用、購入および販売

コミュニティー

Red Hat ドキュメントについて

Red Hat をお使いのお客様が、信頼できるコンテンツが含まれている製品やサービスを活用することで、イノベーションを行い、目標を達成できるようにします。 最新の更新を見る.

多様性を受け入れるオープンソースの強化

Red Hat では、コード、ドキュメント、Web プロパティーにおける配慮に欠ける用語の置き換えに取り組んでいます。このような変更は、段階的に実施される予定です。詳細情報: Red Hat ブログ.

会社概要

Red Hat は、企業がコアとなるデータセンターからネットワークエッジに至るまで、各種プラットフォームや環境全体で作業を簡素化できるように、強化されたソリューションを提供しています。

Theme

© 2025 Red Hat