各コレクションで、コレクションに含まれているオブジェクトは事実上、コレクションの内容を表す、SQL 表内で「this」と名付けられている列の内容になります。
pureQuery は、コレクション内の含まれているオブジェクトのクラスを調べ、それらのオブジェクトのプロパティーの名前を決定します。プロパティー名は SQL 表内の列の名前になり、プロパティー値は SQL 表の対応する行のプロパティー値になります。
例えば、customers と名付けられたコレクション List<Customer> と、以下の Customer Bean の定義を考慮してください。
public Customer {
public Integer custId;
public String name;
public String addressl1;
public String city;
public Integer storeRegion;
}
pureQuery は、このような仮想 SQL 表内のコレクションの内容を表す場合があります。
THIS | CUSTID | NAME | ADDRESSL1 | CITY | REGION |
---|---|---|---|---|---|
Customer object 0 | 001 | Big Customer | 1234 Market Street | San Francisco | 900 |
Customer object 1 | 002 | Another Big Customer | 5678 California Street | San Francisco | 900 |
Customer object 2 | 003 | Still Another Big Customer | 90 North First Street | San Jose | 900 |
Customer オブジェクトのリストを照会し、販売地域内の上位 5 つの大都市にいる顧客を表すそれらのオブジェクトを戻すために、アノテーション付きメソッドを必要としているとします。
次のようなメソッドを作成することができます。
@Select(sql="SELECT cr.custId, cr.name, cr.addressl1, " +
" cr.city, cr.storeRegion " +
" FROM ?1 AS cr, ?2 AS t5 WHERE cr.city = t5.city ")
List<Customer> getMailingList(List<Customer> curRegion,
CitySize[] topFive);
public class CitySize {
public String city;
public Integer size;
}
Customer オブジェクトは curRegion リストから読み取られるので、CitySize 配列に示される都市と同等の都市があるオブジェクトでは、選択された列値が Customer オブジェクトの新規インスタンスにデータを追加するために使用され、新規オブジェクトが getMailingList() メソッドにより戻されるリストに追加されます。
さらに、次のように、Customer オブジェクトのすべての列を選択することもできます。
@Select(sql="SELECT cr.* " +
" FROM ?1 AS cr, ?2 AS t5 WHERE cr.city = t5.city ")
List<Customer> getMailingList(List<Customer> curRegion,
CitySize[] topFive);
Customer オブジェクトのインスタンスは curRegion リストから読み取られるので、CitySize 配列に示される都市と同等の都市があるオブジェクトでは、すべての選択された列値 (列「this」を含む) が Customer オブジェクトの新規インスタンスにデータを追加するために使用され、新規オブジェクトが getMailingList() メソッドにより戻されるリストに追加されます。
この例では、Customer オブジェクトは curRegion リストから読み取られるので、CitySize 配列に示される都市と同等の都市があるオブジェクトは、getMailingList() メソッドにより戻されるリストに追加されます。
@Select(sql="SELECT cr.this FROM ?1 AS cr, ?2 AS t5 " +
" WHERE cr.city = t5.city ")
List<Customer> getMailingList(List<Customer> curRegion,
CitySize[] topFive);
異なるバージョンの getMailingList() メソッド間の相違点は、そのメソッドが List<Map<String,Object>> オブジェクトを戻すように定義されるとさらに明白になります。
最初のバージョンは、各 Map オブジェクトに、値 custid、name、addressl1、city、および storeregion を持つ String キーが含まれるという結果になります。
2 番目のバージョンは、各 Map オブジェクトに、値 this、custid、name、addressl1、city、および storeregion を持つ String キーが含まれるという結果になります。
3 番目のバージョンは、各 Map オブジェクトに、値 this を持つ String キーが含まれるという結果になります。