The Apache DB ProjectObJectRelationalBridge

Jakarta OJB

ダウンロード

チュートリアル

ユーザ ドキュメント

システム ドキュメント

開発

日本語訳 (Translations)

オリジナル

OJB Queries

このチュートリアルでは、異なる問い合わせ(クエリ)メカニズムの利用法を記述します。ここで示されるサンプルコードは、JUnitテストクラスで主に使われます。

目次
標準クエリ

このセクションでは、標準クエリをどのように使用するかを学びます。 標準クエリ用のCriteriaクラス群などは、org.apache.ojb.broker.queryパッケージ内にあります。 標準クエリを使用して、オブジェクト全体へのクエリ (例えば、personテーブル/オブジェクト) あるいは、列データを戻すレポート用クエリ のどちらも問い合わせ可能です。

クエリ(問い合わせ)は、大まかに以下のパーツで成り立っています:

  1. 問い合わせられるオブジェクトのクラス
  2. 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

and / or

All selection criteria added to a criteria set using the above factory methods will be ANDed in the WHERE-clause. To get an OR combination two criteria sets are needed. These sets are combined using addOrCriteria:

  Criteria crit1 = new Criteria();
  crit1.addLike("firstname", "%o%");
  crit1.addLike("lastname", "%m%");
  Criteria crit2 = new Criteria();
  crit2.addEqualTo("firstname", "hank");

  crit1.addOrCriteria(crit2);
  Query q = QueryFactory.newQuery(Person.class, crit1);

  Collection results = broker.getCollectionByQuery(q);

This query will generate an SQL statement like this:
  SELECT ... WHERE (FIRSTNAME LIKE "%o%") AND LASTNAME LIKE "%m%" OR FIRSTNAME = "hank"

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.

joins

Joins resulting from path expressions ("relationship.attribute") in criteria are automatically handled by OJB. Path expressions are supported for all relationships 1:1, 1:n and m:n (decomposed and non-decomposed) and can be nested.

The following sample looks for all articles belonging to the product group "Liquors". Article and product group are linked by the relationship "productGroup" in class Article:

<!-- Definitions for org.apache.ojb.ojb.broker.Article -->
   <class-descriptor
   	  class="org.apache.ojb.broker.Article"
   	  proxy="dynamic"
   	  table="Artikel"
   >
      ...
      <reference-descriptor
         name="productGroup"
         class-ref="org.apache.ojb.broker.ProductGroup"
      >
         <foreignkey field-id-ref="4"/>
      </reference-descriptor>
   </class-descriptor>

   <class-descriptor
   	  class="org.apache.ojb.broker.ProductGroup"
   	  proxy="org.apache.ojb.broker.ProductGroupProxy"
   	  table="Kategorien"
   >
	  ...
      <field-descriptor id="2"
         name="groupName"
         column="KategorieName"
         jdbc-type="VARCHAR"
      />
      ...
   </class-descriptor>

The path expression includes the 1:1 relationship "productGroup" and the attribute "groupName":
  Criteria crit = new Criteria();
  crit.addEqualTo("productGroup.groupName", "Liquors");
  Query q = QueryFactory.newQuery(Article.class, crit);

  Collection results = broker.getCollectionByQuery(q);

querying for objects
report queries
ODMGOQL
JDOクエリ

Copyright © 1999-2007, Apache Software Foundation
Translated into Japanese by Tetsuya Kitahata, powered by Terra-International, Inc.
Original English Page would be found from HERE     --    ApacheNews    ASF プロジェクト一覧
Terra-International, Inc. -- テラ・インターナショナル
Special Thanks -- 【お問い合わせ/テキスト広告】