The next thing we need to know is how this Implementation instance
integrates into our persistence operations.
In the use case UCListAllProducts
we have to retrieve a collection containing all product entries from
the persistent store. To retrieve a collection containing objects
matching some criteria we can use the Object Query Language (OQL) as
specified by ODMG. OQL is quite similar to SQL but provides useful
additions for object-oriented programming, such as path epressions. For
example, a query for persons that live in the city of Amsterdam could
be defined in OQL like follows: "select
person from Person where person.address.town='Amsterdam'".
See
chapter 4 of [ODMG30] for a complete OQL reference.
In our use case we want to select all persistent instances
of the class Product, so our OQL Statement looks rather simple:
"select allproducts from
Product"
. To perform this OQLQuery we have to open a new
ODMG Transaction first.
In the second step we have to obtain a OQLQuery object. As
mentioned before we can use a factory method of the Implementation
object for a new Query object.
In the third step we fill the query object with our special OQL
statement.
In step four we perform the query and collect the results in a
DList (one of the ODMG persistent collection classes). As we don't
have any further db activity we can commit the transaction.
In the last step we iterate through the collection to print out
each product matching our query.
 |  |  |
 |
public void apply()
{
System.out.println("The list of available products:");
try
{
// 1. open a transaction
Transaction tx = odmg.newTransaction();
tx.begin();
// 2. get an OQLQuery object from the ODMG facade
OQLQuery query = odmg.newOQLQuery();
// 3. set the OQL select statement
query.create("select allproducts from " +
Product.class.getName());
// Product.class.getName() is used a convenience to
// retrive the fully qualified name
// 4. perform the query and store the result
// in a persistent Collection
DList allProducts = (DList) query.execute();
tx.commit();
// 5. now iterate over the result to print each product
java.util.Iterator iter = allProducts.iterator();
while (iter.hasNext())
{
System.out.println(iter.next());
}
}
catch (Throwable t)
{
t.printStackTrace();
}
}
|  |
 |  |  |