Jakarta OJB ダウンロード チュートリアル ユーザ ドキュメント システム ドキュメント 開発 日本語訳 (Translations) オリジナル | | OJB Queries |
このチュートリアルでは、異なる問い合わせ(クエリ)メカニズムの利用法を記述します。ここで示されるサンプルコードは、JUnitテストクラスで主に使われます。
| 標準クエリ |
このセクションでは、標準クエリをどのように使用するかを学びます。
標準クエリ用のCriteriaクラス群などは、org.apache.ojb.broker.queryパッケージ内にあります。
標準クエリを使用して、オブジェクト全体へのクエリ (例えば、personテーブル/オブジェクト) あるいは、列データを戻すレポート用クエリ
のどちらも問い合わせ可能です。
クエリ(問い合わせ)は、大まかに以下のパーツで成り立っています:
- 問い合わせられるオブジェクトのクラス
- ORDER BY や GROUP BY を使ったリスト化(訳注:ソートや集計などという、標準クエリにあるもの)
OJBでは、QueryFactoryを使って、新しいクエリを生成します。
クエリ用クラスのコンストラクタは、Publicですが、
QueryFactoryを使って新しいクエリを生成する場合、以下のようにする方が望まれます:
 |  |  |  |
Query q = QueryFactory.newQuery(Person.class, crit);
|  |  |  |  |
各々のcriteria(訳注:以下の例でのcritでの設定)は、SQLでのwhere節の代わりとなります。
 |  |  |  |
Criteria crit = new Criteria();
crit.addEqualTo("firstname", "tom");
crit.addEqualTo("lastname", "hanks");
Query q = QueryFactory.newQuery(Person.class, crit);
|  |  |  |  |
このクエリは、以下のようなSQL文を生成します:
 |  |  |  |
SELECT ... FROM PERSON WHERE FIRSTNAME = "tom" AND LASTNAME = "hanks";
|  |  |  |  |
| query criteria |
OJB provides selection criteria for almost any SQL-comparator.
In most cases you do not have to deal directly with the implementing classes
like EqualToCriteria.
The Criteria class provides factory methods for the appropriate classes.
There are four kinds of factory methods:
- create criteria to compare a field to a value: ie. addEqualTo("firstname", "tom");
- create criteria to compare a field to another field: ie. addEqualToField("firstname", "other_field");
- create criteria to check null value: ie. addIsNull("firstname");
- create a raw sql criteria: ie: addSql("REVERSE(name) like 're%'");
The following list shows some of the factory methods to compare a field to a value:
- addEqualTo
- addLike
- addGreaterOrEqualThan
- addGreaterThan
- addLike
- addBetween , this methods has two value parameters
- addIn , this method uses a Collection as value parameter
- and of course there negative forms
This list shows some factory methods to compare a field to another field, all those methods end on ...field:
- addEqualToField
- addGreaterThanField
- and of course there negative forms
|
| ordering and grouping |
The following methods of Criteria are used for ordering and grouping:
- addOrderByAscending(String anAttributeName);
- addOrderByDescending(String anAttributeName);
- addGroupBy(String anAttributeName); this method is used for report queries
You can of course have multiple order by and group by clauses, simply repeat the addOrderBy.
 |  |  |  |
crit = new Criteria();
crit.addOrderByDescending("id");
crit.addOrderByAscending("lastname");
query = new QueryByCriteria(Person.class, crit);
broker.getCollectionByQuery(query);
|  |  |  |  |
The code snippet will query all Persons and order them by attribute "id" descending and "lastname" ascending.
The query will produce the following SQL-statement using column numbers in the ORDER BY clause:
 |  |  |  |
SELECT A0.ID,A0.FIRSTNAME,A0.LASTNAME FROM PERSON A0 ORDER BY 1 DESC,3
|  |  |  |  |
When you use the column name "LASTNAME" instead of the attribute name
"lastname" (crit.addOrderBy("LASTNAME");), an additional column named "LASTNAME" without alias will be
added.
 |  |  |  |
SELECT A0.ID,A0.FIRSTNAME,A0.LASTNAME,LASTNAME FROM PERSON A0 ORDER BY 1 DESC,4
|  |  |  |  |
If there are multiple tables with a column "LASTNAME" the SQL-Statement will produce an error,
so it's better to always use attribute names.
|
|
|
|