14.10. Expressions 式
where 節で使用する表現には以下が含まれます:
- 算術演算子:
+, -, *, / - 2項比較演算子:
=, >=, <=, <>, !=, like - 論理演算子
and, or, not - グループ分けを表す括弧
( ) in,not in,between,is null,is not null,is empty,is not empty,member ofandnot member of- "シンプル"な case
case ... when ... then ... else ... end、 "探索的"な casecase 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 ...)。ただし使用するデータベースが ANSIcast()と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'、69、6.66E+2、'1970-01-01 10:00:01.0' - Java の
public static final定数:eg.Color.TABBY
in と between は以下のように使用できます:
from DomesticCat cat where cat.name between 'A' and 'B'
from DomesticCat cat where cat.name between 'A' and 'B'
from DomesticCat cat where cat.name in ( 'Foo', 'Bar', 'Baz' )
from DomesticCat cat where cat.name in ( 'Foo', 'Bar', 'Baz' )
また、否定形で以下のように記述することもできます。
from DomesticCat cat where cat.name not between 'A' and 'B'
from DomesticCat cat where cat.name not between 'A' and 'B'
from DomesticCat cat where cat.name not in ( 'Foo', 'Bar', 'Baz' )
from DomesticCat cat where cat.name not in ( 'Foo', 'Bar', 'Baz' )
同様に
is null や is not null は null 値をテストするために使用できます。
Hibernate 設定で HQL query substitutions を宣言すれば、boolean 値を式の中で簡単に使用できます:
<property name="hibernate.query.substitutions">true 1, false 0</property>
<property name="hibernate.query.substitutions">true 1, false 0</property>
こうすることで下記の HQL を SQL に変換するときに
true 、 false キーワードは 1 、 0 に置き換えられます:
from Cat cat where cat.alive = true
from Cat cat where cat.alive = true
特別なプロパティ
size、または特別な関数 size() を使ってコレクションのサイズをテストできます。
from Cat cat where cat.kittens.size > 0
from Cat cat where cat.kittens.size > 0
from Cat cat where size(cat.kittens) > 0
from Cat cat where size(cat.kittens) > 0
インデックス付きのコレクションでは、
minindex と maxindex 関数を使って、インデックスの最小値と最大値を参照できます。同様に、 minelement と maxelement 関数を使って、基本型のコレクションの最小要素と最大要素を参照できます。
from Calendar cal where maxelement(cal.holidays) > current_date
from Calendar cal where maxelement(cal.holidays) > current_date
from Order order where maxindex(order.items) > 100
from Order order where maxindex(order.items) > 100
from Order order where minelement(order.items) > 10000
from Order order where minelement(order.items) > 10000
コレクションの要素やインデックスのセット(
elements と indices 関数)、または副問い合わせの結果が受け取れるときは、SQL 関数 any, some, all, exists, in がサポートされます(以下参照)。
select mother from Cat as mother, Cat as kit where kit in elements(foo.kittens)
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 p from NameList list, Person p
where p.name = some elements(list.names)
from Cat cat where exists elements(cat.kittens)
from Cat cat where exists elements(cat.kittens)
from Player p where 3 > all elements(p.scores)
from Player p where 3 > all elements(p.scores)
from Show show where 'fizard' in indices(show.acts)
from Show show where 'fizard' in indices(show.acts)
size、elements、indices、minindex、maxindex、minelement、maxelement は Hibernate3 の where 節だけで利用可能であることに注意してください。
インデックス付きのコレクション(arrays, lists, maps)の要素は、where節内でのみインデックスで参照できます:
from Order order where order.items[0].id = 1234
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 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[ 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[ maxindex(order.items) ] = item and order.id = 11
[] 内部の式は、算術式の場合もあります。
select item from Item item, Order order where order.items[ size(order.items) - 1 ] = item
select item from Item item, Order order
where order.items[ size(order.items) - 1 ] = item
HQL は一対多関連や値のコレクションの要素に対して、組み込みの
index() 関数も用意しています。
select item, index(item) from Order order
join order.items item
where index(item) < 5
select item, index(item) from Order order
join order.items item
where index(item) < 5
ベースとなるデータベースがサポートしているスカラ SQL 関数が使用できます:
from DomesticCat cat where upper(cat.name) like 'FRI%'
from DomesticCat cat where upper(cat.name) like 'FRI%'
下のクエリがSQL ではどれだけ長く、読みづらくなるか考えてください:
ヒント: 例えばこのように出来ます。