If there is a 10,000 records in the Product table, you get all the records from the repository is a very time time.
For each record, you must create a new object, and the entire table reads into memory. In the sample program, there is no consideration
Can problems, but in an actual application OJB program, we need a more effective way to get all the records.
Record, if your program does not need to transfer the entire table to memory, then it is recommended that you use the GetITeratorbyQuery ()
The law returns to Iterator without returning Collection.
If you need to spread a larger result set, this method is very practical. All instances are not one-time creation, just
The memory is allocated when you "need". The storage structure instance that the user is no longer used can be Garbage Collecto
r reclaimed. Below is an instance code of this method:
Public void apply ()
{
System.out.println ("The List of Available Products);
// Build a Query That Select All Objects of Class Product,
// welch any further criteria aciding to odmg
// The Collection Containing All
// instances of a personistent class is caled "eXtent"
Query Query = New QuerybyCriteria (Product.class, NULL);
Try
{
// ask the Broker to Retrieve An Iterator
Java.util.iterator it = broker.getiteratorbyqury (query);
// Now Itereate over the result to print each product
While (item.hasnext ())
{
System.out.println (iter.next ());
}
}
Catch (throwable t)
{
T.PrintStackTrace ();
}
}
More detailed instructions can refer to PersistenceBroker Javadoc and Query Doc.
Storage object:
Let us now look at the ucenternewproduct class. It works this way: First, it creates a new pair
Icon, then ask the user to enter the relevant data of the new product (product name, price, inventory). These data are deposited
Stored in a new object. Then we have to save the newly created objects into the repository. We can use 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 (PersistenceBrokeexception EX)
{
// if Something Wrent WRONG: ROLLBACK
Broker.aborttransaction ();
System.out.println (ex.getMessage ());
EX.PrintStackTrace ();
}
}
Maybe you have discovered, we don't have a new product's primary key ID assignment. OJB can check the ID of a new product is not
There is a unique ID value to be set to be set. The automatic growth of the ID value is defined in the Repository-XML.
Update object:
When the user edits a product (by selecting 2 from the directory), the user must enter the ID value to edit the product.
. Because the program does not maintain the product object table, the system must first pass the PersistenceBroker from the repository.
Go to a product.
Choose an object via PersistenceBroker is simple - we must first create a QueryByCriter
Ia object. QueryByCriteria object contains ID value information entered by the user, you may have a child, we don't need a product.
Other information, we only need to set a filter for ProductID. Build a criteria object directly
To make you declare a complex condition such as the ProductID value must be greater than 2 less than 5. Complex query will be later
Introduce.
Once the product object is obtained through the Broker.GetObjectByQuery (Query) method, it will pass
User input to modify the object properties, and then save the modified structural storage through Broker.Store (TobeEDITED)
Reservoir. Below is the relevant code for the UceditProduct class:
Public void apply ()
{
String in = ReadlineWithMessage ("Edit Product with ID:");
INT ID = integer.parseint (in);
// We do Not Have a reference to the successd 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 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 (TobeEd);
// 7. Commit Transaction
Broker.committransaction ();
}
Catch (throwable t)
{
// rollback in case of errors
Broker.aborttransaction ();
T.PrintStackTrace ();
}
}