如果在product表中有10000条记录,那么从存储库中获得所有得记录是一个很费时的操  
作,每个记录都必须新建一个对象,整个表都要读入内存。在示例程序中,没有考虑性  
能问题,但是在一个实际的应用OJB的程序中,我们需要一个更有效的方法来获得所有记  
录,如果你的程序不需要将整个表调入内存,那么建议你使用getIteratorByQuery()方  
法来返回Iterator而不返回Collection。  
如果你需要遍历较大的结果集,这种方法很实用。所有实例并不是一次性被创建,只是  
在你"需要"的时候才分配内存。用户不再使用的存储结构实例可以被garbage collecto  
r重新回收。下面就是使用这种方法的实例代码:  
public void apply()  
{  
    System.out.println("The list of available products:");  
    // build a query that select all objects of Class Product,  
    // without any further criteria according to ODMG  
    // the Collection containing all  
    // instances of a persistent class is called "Extent"  
    Query query = new QueryByCriteria(Product.class, null);  
    try  
    {  
        // ask the broker to retrieve an Iterator  
        java.util.Iterator iter = broker.getIteratorByQuery(query);  
        // now iterate over the result to print each product  
        while (iter.hasNext())  
        {  
            System.out.println(iter.next());  
        }  
    }  
    catch (Throwable t)  
    {  
        t.printStackTrace();  
    }  
}  
更详细的说明可以参考PersistenceBroker JavaDoc和Query Doc。  
存储对象:  
现在让我们来看看UCEnterNewProduct类。它是这样来工作的:首先,它创建一个新的对  
象,然后要求用户输入新产品的相关数据(产品名称,价格,库存量)。这些数据被存  
储在新对象中。然后我们必须把新创建的对象存到存储库中。我们可以使用Persistenc  
eBroker.store(Object ojb):  
public void apply()  
{  
    // this will be our new object  
    Product newProduct = new Product();  
    // now read in all relevant information and fill the new object:  
    System.out.println("please enter a new product");  
    String in = readLineWithMessage("enter name:");  
    newProduct.setName(in);  
    in = readLineWithMessage("enter price:");  
    newProduct.setPrice(Double.parseDouble(in));  
    in = readLineWithMessage("enter available stock:");  
    newProduct.setStock(Integer.parseInt(in));  
    // now perform persistence operations  
    try  
    {  
        // 1. open transaction  
        broker.beginTransaction();  
        // 2. make the new object persistent  
        broker.store(newProduct);  
        broker.commitTransaction();  
    }  
    catch (PersistenceBrokerException ex)  
    {  
        // if something went wrong: rollback  
        broker.abortTransaction();  
        System.out.println(ex.getMessage());  
        ex.printStackTrace();  
    }  
}  
可能你已经发现了,我们并没有新产品的主键id赋值。OJB能够检查到一个新产品的id没  
有被设置而自动制定一个唯一的id值。Id值的自动增长是在Repository-XML中定义的。  
   
更新对象:  
当用户相编辑一个产品的时候(通过从目录中选择2),用户必须输入想编辑产品的id值  
。因为程序没有维护着产品对象表,系统必须首先通过PersistenceBroker从存储库中得  
到一个产品。  
通过PersistenceBroker选择一个对象很简单――我们必须首先创建一个QueryByCriter  
ia对象。QueryByCriteria对象含有用户输入的id值信息,你可能发祥我们并不需要产品  
的其他信息,我们只需要对productId设置过滤器就行了。直接构建一个Criteria对象可  
以让你申明一个复杂的条件如productId值必须大于2小于5。复杂的查询在本文的后面将  
介绍。  
一旦通过broker.getObjectByQuery(query)方法获得了Product对象,接下来就能够通过  
用户输入来修改对象属性,然后通过broker.store(toBeEdited)将修改后的结构存入存  
储库。下面是UCEditProduct类的有关代码:  
public void apply()  
{  
    String in = readLineWithMessage("Edit Product with id:");  
    int id = Integer.parseInt(in);  
    // We do not have a reference to the selected Product.  
    // So first we have to lookup the object,  
    // we do this by a query by example (QBE):  
    // 1. build an example object with matching primary key values:  
    Product example = new Product();  
    example.setId(id);  
    // 2. build a QueryByCriteria from this sample instance:  
    Query query = new QueryByCriteria(example);  
    try  
    {  
        // 3. start broker transaction  
        broker.beginTransaction();  
        // 4. lookup the product specified by the QBE  
        Product toBeEdited = (Product) broker.getObjectByQuery(query);  
        // 5. edit the existing entry  
        System.out.println("please edit the product entry");  
        in = readLineWithMessage(  
                "enter name (was " + toBeEdited.getName() + "):");  
        toBeEdited.setName(in);  
        in = readLineWithMessage(  
                "enter price (was " + toBeEdited.getPrice() + "):");  
        toBeEdited.setPrice(Double.parseDouble(in));  
        in = readLineWithMessage(  
                "enter available stock (was " +  
                    toBeEdited.getStock()+ "):");  
        toBeEdited.setStock(Integer.parseInt(in));  
        // 6. now ask broker to store the edited object  
        broker.store(toBeEdited);  
        // 7. commit transaction  
        broker.commitTransaction();  
    }  
    catch (Throwable t)  
    {  
        // rollback in case of errors  
        broker.abortTransaction();  
        t.printStackTrace();  
    }  
}   
 
  |