11.4.8. 限定パス式について
コレクション値 (collection-valued) のアソシエーションは、実際にはそのコレクションの値を参照すると前項で説明しました。コレクションのタイプを基に、明示的な限定式のセットも使用可能です。
表11.9 限定パス式
式 | 説明 |
---|---|
VALUE |
コレクション値を参照します。限定子を指定しないことと同じです。目的を明示的に表す場合に便利です。コレクション値 (collection-valued) の参照のすべてのタイプに対して有効です。
|
INDEX |
HQL ルールによると、マップキーまたはリストの場所 (OrderColumn の値) へ参照するよう
javax.persistence.OrderColumn を指定するマップとリストに対して有効です。JPQL では List の使用に対して確保され、MAP に対して KEY を追加します。JPA プロバイダーの移植性に関心があるアプリケーションは、この違いに注意する必要があります。
|
KEY |
マップに対してのみ有効です。マップのキーを参照します。キー自体がエンティティーである場合、更にナビゲートすることが可能です。
|
ENTRY |
マップに対してのみ有効です。マップの論理
java.util.Map.Entry タプル (キーと値の組み合わせ) を参照します。ENTRY は終端パスとしてのみ有効で、select 節のみで有効になります。
|
例11.9 限定コレクション参照の例
// Product.images is a Map<String,String> : key = a name, value = file path // select all the image file paths (the map value) for Product#123 select i from Product p join p.images i where p.id = 123 // same as above select value(i) from Product p join p.images i where p.id = 123 // select all the image names (the map key) for Product#123 select key(i) from Product p join p.images i where p.id = 123 // select all the image names and file paths (the 'Map.Entry') for Product#123 select entry(i) from Product p join p.images i where p.id = 123 // total the value of the initial line items for all orders for a customer select sum( li.amount ) from Customer c join c.orders o join o.lineItems li where c.id = 123 and index(li) = 1