この例では、コレクションは java.util.List オブジェクトであり、java.lang.Iterable インターフェースのインプリメンテーションです。 Customer オブジェクトのマテリアライズされた List を戻す場合、接続済みデータ・ソースに再アクセスせずに、データに関する複数の質問を尋ねることができます。
さらに、サンフランシスコのベイエリア販売地域にいる顧客の人数を知りたいとします。 コードは、以下のようになります。
Connection con = DriverManager.getConnection(...); 1
Data db = DataFactory.getData(con); 2
Data qocdata = DataFactory.getData(); 3
List<Customer> customers = db.queryList( 4
"SELECT * from SALES.CUSTOMER", Customer.class);
db.close(); 5
int sanFranCode = ... 6
Integer countCity = qocdata.queryFirst( 7
"SELECT COUNT(*) FROM ?1.com.company.Customer AS cr WHERE cr.storeRegion = ?2",
Integer.class, customers, sanFranCode );
qocdata.close(); 8
このコードは、以下のステップを実行します。
qocdata を閉じる代わりに、次の例で示すような、顧客についての追加の分析を実行することもできます。
経営者に対するレポートとして、サンフランシスコのベイエリア販売地域にある個別の都市数と、その地域の (異なるアドレス数という観点からの) 上位 5 つの大都市を知る必要があるとします。 それにより、経営者の許可があれば、メール・キャンペーンでそれらの都市の顧客をターゲットにします。
接続済みのデータ・ソースに対する 1 つの照会と、アノテーション付きメソッドを使用する少量のアプリケーション・ロジックを使用して、コレクションに対する 3 つの照会を結合させることができます。
最初に、呼び出しアプリケーションとアノテーション付きメソッドの両方で使用されるステージング・クラスが必要です。
public class CitySize {
public String city;
public Integer size;
}
インプリメンテーション・クラスのメソッドを、コレクションに対する照会とデータベースに対する照会の両方には使用できないので、2 つの別個のインターフェースを作成する必要があります。
CustomerQuery インターフェースは、データ・ソースを照会するためのメソッドを定義します。 getCustomersInRegion() メソッドは、指定された販売領域内にいる顧客を表す Customer オブジェクトのリストを戻します。
import com.company.Customer;
public interface CustomerQuery
{
@Select(sql=
"SELECT custId, name, ... FROM Customer WHERE region=?1")
List<Customer> getCustomersInRegion(int r);
}
RegionReport インターフェースは、コレクション・オブジェクトを照会するためのメソッドを定義します。
import com.company.Customer;
import com.company.Campaign.CitySize;
public interface RegionReport
{
@Select(sql="SELECT COUNT(DISTINCT city) FROM ?1")
public Integer countCities (List<Customer> curRegion);
@Select(sql=
"SELECT city, COUNT(DISTINCT addressL1) AS size " +
" FROM ?1 GROUP BY city ORDER BY size DESC")
public CitySize[] getCitySize(List<Customer> curRegion);
@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);
}
インターフェースのインプリメンテーション・クラスを生成した後に、経営者へのレポートと、以下のようなアプリケーション・ロジックによるターゲット顧客のリストを作成することができます。
Connection con = DriverManager.getConnection(...); 1
CustomerQuery cQuery =
DataFactory.getData( CustomerQuery.class, con ); 2
RegionReport inMem =
DataFactory.getData( RegionReport.class ); 3
int sanFranCode = ...; 4
List<Customer> customers = cQuery.getCustomersInRegion(sanFranCode); 5
cQuery.close(); 6
Integer cityCount = inMem.countCities(customers); 7
System.out.println ( 8
" There are " + cityCount + " cities in region " + sanFranCode );
System.out.println (
" The largest 5, and their number of addresses are: " );
CitySize[] allCityList = inMem.getCitySize(customers); 9
CitySize[] topFive = new CitySize[5]; 10
for (int i=0; i<5; i++) 11
{ topFive[i] = allCityList[i];
System.out.println (
" " + topFive[i].city + " " + topFive[i].size);
}
List<Customer> mailingCust = 12
inMem.getMailingList(customers, topFive);
inMem.close(); 13
このコードは、以下のステップを実行します。
注目すべき点として、経営者が、ターゲット・メーリングが少なすぎるか多すぎると判断している場合には、最終的な mailingCust リストの Customer オブジェクトの追加または削除には、CitySize 配列の割り振りサイズのごくわずかな変更と、それにデータを追加するループだけが必要です。 getMailingList() メソッドは、その入力配列のサイズには依存していません。