Sophie

Sophie

distrib > Fedora > 13 > i386 > by-pkgid > fffbd1c50e8102746e8aec13b7d53ba8 > files > 39

castor-doc-0.9.5-5.fc12.1.i686.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html><head><title>Using Castor JDO</title><link rel="stylesheet" href="default.css"></head><body bgcolor="#ffffff" link="#6763a9" vlink="#6763a9" topmargin="0" bottommargin="0" leftmargin="0" marginheight="0" marginwidth="0"><a name="top"></a><table border="0" cellpadding="0" cellspacing="0" height="400"><tr><td width="10" valign="top" align="left" bgcolor="#7270c2"><img src="images/dotTrans.gif" width="1" height="1" border="0"></td><td width="115" valign="top" align="left" bgcolor="#7270c2"><img src="images/dotTrans.gif" width="1" height="1" border="0"></td><td width="7" valign="top" align="left"><img src="images/dotTrans.gif" border="0" width="1" height="1"></td><td width="70" valign="top" align="left"><img src="images/dotTrans.gif" width="70" height="6" border="0"></td><td width="400" valign="top" align="left"><img src="images/top_2.gif" width="400" height="6" border="0"></td><td width="120" valign="top" align="left"><img src="images/line_purple.gif" width="120" height="6" border="0"></td></tr><tr><td width="10" bgcolor="#7270c2" valign="top" align="left"><img src="images/dotTrans.gif" border="0" width="1" height="1"></td><td width="115" bgcolor="#7270c2" valign="top" align="left"><img src="images/dotTrans.gif" border="0" width="1" height="1"></td><td width="7" bgcolor="#ffffff" valign="top" align="left"></td><td width="70" valign="top" align="left"><img src="images/dotTrans.gif" width="1" height="1" border="0"></td><td width="400" valign="middle" align="left"><a href="http://www.exolab.org"><span class="menuTopOff">ExoLab</span></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="http://openejb.sf.net"><span class="menuTopOff">OpenEJB</span></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="http://openjms.sf.net"><span class="menuTopOff">OpenJMS</span></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="http://openorb.sf.net"><span class="menuTopOff">OpenORB</span></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="http://castor.exolab.org"><span class="menuTopOn">Castor</span></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="http://tyrex.sf.net"><span class="menuTopOff">Tyrex</span></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br><img src="images/dotTrans.gif" width="1" height="2" border="0"></td><td width="120" height="20" valign="top" align="left">&nbsp;</td></tr><tr><td width="10" bgcolor="#7270c2" valign="top" align="left"><img src="images/dotTrans.gif" width="10" height="3" border="0"></td><td width="115" bgcolor="#7270c2" valign="top" align="right"><img src="images/line_sm.gif" width="105" height="3" border="0"></td><td width="7" bgcolor="#a9a5de" valign="top" align="left"><img src="images/line_sm.gif" width="7" height="3" border="0"></td><td width="70" valign="top" align="left"><img src="images/line_light.gif" width="70" height="3" border="0"></td><td width="400" valign="top" align="left"><img src="images/line_light.gif" width="400" height="3" border="0"></td><td width="120" valign="top" align="left"><img src="images/dotTrans.gif" border="0" width="1" height="1"></td></tr><tr><td bgcolor="#7270c2" valign="top" align="left"><img src="images/dotTrans.gif" width="10" height="10" border="0"></td><td width="115" bgcolor="#7270c2" valign="top" align="left"><img src="images/dotTrans.gif" width="1" height="2" border="0"><br><table border="0" cellpadding="0" cellspacing="0"><tr><td valign="top" align="left"><span class="subMenuOn">Main</span></td></tr><tr><td valign="top" align="left">&nbsp;&nbsp;<a href="index.html"><span class="subMenuOff">Home</span></a></td></tr><tr><td valign="top" align="left">&nbsp;&nbsp;<a href="download.html"><span class="subMenuOff">Download</span></a></td></tr><tr><td valign="top" align="left">&nbsp;&nbsp;<a href="api/overview-summary.html"><span class="subMenuOff">API</span></a></td></tr><tr><td valign="top" align="left">&nbsp;&nbsp;<a href="schema.html"><span class="subMenuOff">Schema</span></a></td></tr><tr><td valign="top" align="left">&nbsp;&nbsp;<a href="lists.html"><span class="subMenuOff">Mailing Lists</span></a></td></tr><tr><td valign="top" align="left">&nbsp;&nbsp;<a href="cvs.html"><span class="subMenuOff">CVS / Bugzilla</span></a></td></tr><tr><td valign="top" align="left">&nbsp;&nbsp;<a href="support.html"><span class="subMenuOff">Support</span></a></td></tr></table><br><table border="0" cellpadding="0" cellspacing="0"><tr><td valign="top" align="left"><span class="subMenuOn">XML</span></td></tr><tr><td valign="top" align="left">&nbsp;&nbsp;<a href="xml-framework.html"><span class="subMenuOff">Using XML</span></a></td></tr><tr><td valign="top" align="left">&nbsp;&nbsp;<a href="sourcegen.html"><span class="subMenuOff">Source Generator</span></a></td></tr><tr><td valign="top" align="left">&nbsp;&nbsp;<a href="xmlschema.html"><span class="subMenuOff">Schema Support</span></a></td></tr><tr><td valign="top" align="left">&nbsp;&nbsp;<a href="xml-mapping.html"><span class="subMenuOff">XML Mapping</span></a></td></tr><tr><td valign="top" align="left">&nbsp;&nbsp;<a href="xml-faq.html"><span class="subMenuOff">XML FAQ</span></a></td></tr></table><br><table border="0" cellpadding="0" cellspacing="0"><tr><td valign="top" align="left"><span class="subMenuOn">JDO</span></td></tr><tr><td valign="top" align="left">&nbsp;&nbsp;<a href="jdo.html"><span class="subMenuOff">Using JDO</span></a></td></tr><tr><td valign="top" align="left">&nbsp;&nbsp;<a href="database-conf.html"><span class="subMenuOff">JDO Config</span></a></td></tr><tr><td valign="top" align="left">&nbsp;&nbsp;<a href="types.html"><span class="subMenuOff">Types</span></a></td></tr><tr><td valign="top" align="left">&nbsp;&nbsp;<a href="jdo-mapping.html"><span class="subMenuOff">JDO Mapping</span></a></td></tr><tr><td valign="top" align="left">&nbsp;&nbsp;<a href="jdo-faq.html"><span class="subMenuOff">JDO FAQ</span></a></td></tr><tr><td valign="top" align="left">&nbsp;&nbsp;<a href="castor-one.html"><span class="subMenuOff">Other Features</span></a></td></tr></table><br><table border="0" cellpadding="0" cellspacing="0"><tr><td valign="top" align="left"><span class="subMenuOn">Advanced JDO</span></td></tr><tr><td valign="top" align="left">&nbsp;&nbsp;<a href="oql.html"><span class="subMenuOff">OQL</span></a></td></tr><tr><td valign="top" align="left">&nbsp;&nbsp;<a href="locking.html"><span class="subMenuOff">Trans. &amp; Locks</span></a></td></tr><tr><td valign="top" align="left">&nbsp;&nbsp;<a href="design-persist.html"><span class="subMenuOff">Design</span></a></td></tr><tr><td valign="top" align="left">&nbsp;&nbsp;<a href="key-generator.html"><span class="subMenuOff">KeyGen</span></a></td></tr><tr><td valign="top" align="left">&nbsp;&nbsp;<a href="long-transact.html"><span class="subMenuOff">Long Trans.</span></a></td></tr><tr><td valign="top" align="left">&nbsp;&nbsp;<a href="nested-attr.html"><span class="subMenuOff">Nested Attrs.</span></a></td></tr><tr><td valign="top" align="left">&nbsp;&nbsp;<a href="pooling.html"><span class="subMenuOff">Pooling Examples</span></a></td></tr><tr><td valign="top" align="left">&nbsp;&nbsp;<a href="postgresql-blobs.html"><span class="subMenuOff">Blobs and PostgreSQL</span></a></td></tr></table><br><table border="0" cellpadding="0" cellspacing="0"><tr><td valign="top" align="left"><span class="subMenuOn">More</span></td></tr><tr><td valign="top" align="left">&nbsp;&nbsp;<a href="presentations.html"><span class="subMenuOff">Presentations</span></a></td></tr><tr><td valign="top" align="left">&nbsp;&nbsp;<a href="examples.html"><span class="subMenuOff">The Examples</span></a></td></tr><tr><td valign="top" align="left">&nbsp;&nbsp;<a href="extras.html"><span class="subMenuOff">Extras and 3rd Party Tools</span></a></td></tr><tr><td valign="top" align="left">&nbsp;&nbsp;<a href="test-framework.html"><span class="subMenuOff">Test Framework -- JDO</span></a></td></tr><tr><td valign="top" align="left">&nbsp;&nbsp;<a href="ctf.html"><span class="subMenuOff">Test Framework -- XML</span></a></td></tr><tr><td valign="top" align="left">&nbsp;&nbsp;<a href="conf-lib.html"><span class="subMenuOff">Configuration</span></a></td></tr><tr><td valign="top" align="left">&nbsp;&nbsp;<a href="tips-tricks.html"><span class="subMenuOff">Tips &amp; Tricks</span></a></td></tr><tr><td valign="top" align="left">&nbsp;&nbsp;<a href="javadoc/overview-summary.html"><span class="subMenuOff">Full JavaDoc</span></a></td></tr></table><br><table border="0" cellpadding="0" cellspacing="0"><tr><td valign="top" align="left"><span class="subMenuOn">About</span></td></tr><tr><td valign="top" align="left">&nbsp;&nbsp;<a href="license.html"><span class="subMenuOff">License</span></a></td></tr><tr><td valign="top" align="left">&nbsp;&nbsp;<a href="contributors.html"><span class="subMenuOff">Contributors</span></a></td></tr><tr><td valign="top" align="left">&nbsp;&nbsp;<a href="status.html"><span class="subMenuOff">Status, Todo</span></a></td></tr><tr><td valign="top" align="left">&nbsp;&nbsp;<a href="changelog.html"><span class="subMenuOff">Changelog</span></a></td></tr><tr><td valign="top" align="left">&nbsp;&nbsp;<a href="library.html"><span class="subMenuOff">Library</span></a></td></tr><tr><td valign="top" align="left">&nbsp;&nbsp;<a href="contacts.html"><span class="subMenuOff">Contact</span></a></td></tr></table><br></td><td width="7" bgcolor="#a9a5de" valign="top" align="left">&nbsp;</td><td width="70" valign="top" align="left">&nbsp;</td><td rowspan="4" width="400" valign="top"><table cols="2" rows="2" border="0" cellpadding="0" cellspacing="0" width="400"><tr><td valign="top" align="left"><br><img border="0" height="34" hspace="0" src="images/castor.gif" vspace="0" width="115"><br><img border="0" height="10" hspace="0" src="images/dotTrans.gif"></td><td width="120" height="5" valign="top" align="right"><a href="http://www.exolab.org"><img src="images/logo_exolab.gif" hspace="0" vspace="10" width="77" height="20" border="0"></a></td></tr></table><p></p><p></p><br><p><span class="bodyGrey">Reference: <a href="api/org/exolab/castor/jdo/package-summary.html"><api package="org.exolab.castor.jdo">The Java Data Objects API</api></a></span></p><span class="bodyGrey"><a href="#Opening-A-JDO-Database">Opening A JDO Database</a><br></span><span class="bodyGrey"><a href="#Client-Application">Client Application</a><br></span><span class="bodyGrey"><a href="#J2EE-Application">J2EE Application</a><br></span><span class="bodyGrey"><a href="#Using-A-JDO-Database">Using A JDO Database</a><br></span><span class="bodyGrey"><a href="#Transient-And-Persistent-Objects">Transient And Persistent Objects</a><br></span><span class="bodyGrey"><a href="#OQLQuery">OQLQuery</a><br></span><span class="bodyGrey"><a href="#Create/remove/update">Create/remove/update</a><br></span><span class="bodyGrey"><a href="#Using-JDO-And-XML">Using JDO And XML</a><br></span><br><a name="Opening-A-JDO-Database"><h2>Opening A JDO Database</h2></a>        <p><span class="bodyGrey">Castor JDO supports two type of environments, client applications and J2EE servers.          <a href="#Client Application">Client applications</a> are responsible for configuraing          the database connection and managing transactions explicitly. <a href="#J2EE Application">J2EE          applications</a> use JNDI to obtain a pre-configured database connection, and use          <tt>UserTransaction</tt> or container managed transactions (CMT) to manage transactions.          If you have been using JDBC in these two environments, you will be readily familiar with          the two models and the differences between them.</span></p>        <a name="Client-Application"><h3>Client Application</h3></a>          <p><span class="bodyGrey">Client applications are responsible for defining the JDO database configuration,            and managing the transaction explicitly. The database is configured through a            separate XML file which links to the mapping file. In the example code I refer to the            database file as <tt>database.xml</tt>, but any name can be used. See            <a href="database-conf.html">Castor JDO Database Configuration</a> for more information.</span></p>          <p><span class="bodyGrey"><a href="api/org/exolab/castor/jdo/JDO.html"><api>org.exolab.castor.jdo.JDO</api></a> defines the database name and properties and is used            to open a database connection. The database configuration is loaded on demand by setting            the configuration file URL with <tt>setConfiguration</tt>. Creating multiple JDO objects            with the same configuration will only load the database configuration once.</span></p>          <p><span class="bodyGrey">The <a href="api/org/exolab/castor/jdo/Database.html"><api>org.exolab.castor.jdo.Database</api></a> object represents an open connection to the            database. By definition the database object is not thread safe and should not be used from            concurrent threads. There is little overhead involved in opening multiple <tt>Database</tt>            objects, and a JDBC connection is acquired only per open transaction.</span></p>           <p><span class="bodyGrey">The following code snippet opens a database, performs a transaction, and closes the database,             as it will typically appear in client applications:             <span class="bodyBlack"><pre>
JDO      jdo;
Database db;

<span class="bodyGrey"><font color="red">// Define the JDO object</font></span>
jdo = new JDO();
jdo.setDatabaseName( &quot;mydb&quot; );
jdo.setConfiguration( &quot;database.xml&quot; );
jdo.setClassLoader( getClass().getClassLoader() );

<span class="bodyGrey"><font color="red">// Obtain a new database</font></span>
db = jdo.getDatabase();
<span class="bodyGrey"><font color="red">// Begin a transaction</font></span>
db.begin();
<span class="bodyGrey"><font color="red">// Do something</font></span>
. . .
<span class="bodyGrey"><font color="red">// Commit the transaction, close database</font></span>
db.commit();
db.close();
           </pre></span></span></p>                <a name="J2EE-Application"><h3>J2EE Application</h3></a>          <p><span class="bodyGrey"><b>Note:</b> We are now working on supporting Castor inside a J2EE container. Stay            tuned for more information.</span></p>          <p><span class="bodyGrey">J2EE applications depend on the J2EE container (Servlet, EJB, etc) to configure            the database connection and use JNDI to look it up. This model allows the application            deployer to configure the database properties from a central place, and gives the J2EE            container the ability to manage distributed transactions across multiple data sources.</span></p>          <p><span class="bodyGrey">Instead of constructing a <a href="api/org/exolab/castor/jdo/JDO.html"><api>org.exolab.castor.jdo.JDO</api></a> the application uses            the JNDI namespace to look it up. We recommend enlisting the <tt>JDO</tt> object under            the <tt>java:comp/env/jdo</tt> namespace, compatible with the convention for listing JDBC            resources.</span></p>          <p><span class="bodyGrey">The following code snippet uses JNDI to lookup a database, and uses <tt>UserTransaction</tt>            to manage the transaction:            <span class="bodyBlack"><pre>
InitialContext  ctx;
UserTransaction ut;
Database        db;

<span class="bodyGrey"><font color="red">// Lookup databse in JNDI</font></span>
ctx = new InitialContext();
db = (Database) ctx.lookup( &quot;java:comp/env/jdo/mydb&quot; );

<span class="bodyGrey"><font color="red">// Begin a transaction</font></span>
ut = (UserTransaction) ctx.lookup( &quot;java:comp/UserTransaction&quot; );
ut.begin();
<span class="bodyGrey"><font color="red">// Do something</font></span>
. . .
<span class="bodyGrey"><font color="red">// Commit the transaction, close database</font></span>
ut.commit();
db.close();
           </pre></span></span></p>          <p><span class="bodyGrey">When the transaction is managed by the container, a common case with EJB beans and            in particular entity beans, there is no need to being/commit the transaction explicitly.            Instead the application server takes care of enlisting the database in the ongoing            transaction and commiting/rollingback at the proper time.</span></p>          <p><span class="bodyGrey">The following code snippet relies on the container to manage the transaction:            <span class="bodyBlack"><pre>
InitialContext  ctx;
UserTransaction ut;
Database        db;

<span class="bodyGrey"><font color="red">// Lookup databse in JNDI</font></span>
ctx = new InitialContext();
db = (Database) ctx.lookup( &quot;java:comp/env/jdo/mydb&quot; );

<span class="bodyGrey"><font color="red">// Do something</font></span>
. . .
<span class="bodyGrey"><font color="red">// Close the database</font></span>
db.close();
           </pre></span></span></p>              <a name="Using-A-JDO-Database"><h2>Using A JDO Database</h2></a>        <a name="Transient-And-Persistent-Objects"><h3>Transient And Persistent Objects</h3></a>          <p><span class="bodyGrey">All JDO operations occur within the context of a transaction. JDO works by loading            data form the database into an object in memory, allowing the application to modify            the object, and then storing the object's new state when the transaction commits.            All objects can be in one of two states: transient or persistent.</span></p>          <p><span class="bodyGrey"><b>Transient:</b> Any object whose state will not be saved to the database when            the transaction commits. Changes to transient objects will not be reflected in            the database.</span></p>          <p><span class="bodyGrey"><b>Persistent:</b> Any object whose state will be saved to the database when            the transaction commits. Changes to persistent objects will be reflected in the            database.</span></p>          <p><span class="bodyGrey">An object becomes persistent in one of two ways: it is the result of a query,            (and the query is not performed in read-only mode) or it is added to the database             using <a href="api/org/exolab/castor/jdo/Database.html#create(java.lang.Object)">create(java.lang.Object)</a>            or <a href="api/org/exolab/castor/jdo/Database.html#update(java.lang.Object)">update(java.lang.Object)</a>.            All objects that are not persistent are transient. When the transaction commits or            rolls back, all persistent objects become transient.</span></p>         <p><span class="bodyGrey">In a client application, use <a href="api/org/exolab/castor/jdo/Database.html#begin()">begin()</a>,           <a href="api/org/exolab/castor/jdo/Database.html#commit()">commit()</a> and           <a href="api/org/exolab/castor/jdo/Database.html#rollback()">rollback()</a> to manage transactions.           In a J2EE application, JDO relies on the container to manage transactions either           implicitly (based on the transaction attribute of a bean) or explicitly using the           <tt>javax.transaction.UserTransaction</tt> interface.</span></p>         <p><span class="bodyGrey">If a persistent object was modified during the transaction, at commit time the modifications           are stored back to the database. If the transaction rolls back, no modifications will be           made to the database. Once the transaction completes, the object is once again transient.           To use the same object in two different transactions, you must query it again.</span></p>         <p><span class="bodyGrey">An object is transient or persistent from the view point of the database to which           the transaction belongs. An object is generally persistent in a single database,           and calling <a href="api/org/exolab/castor/jdo/Database.html#isPersistent(java.lang.Object)">isPersistent(java.lang.Object)</a>           from another database will return false. It is possible to make an object persistent           in two database, e.g. by querying it in one, and creating it in the other.</span></p>                <a name="OQLQuery"><h3>OQLQuery</h3></a>          <p><span class="bodyGrey">OQL queries are used to lookup and query objects from the database. OQL queries are            similar to SQL queries, but use object names instead of SQL names and do not require            join clauses. For example, if the object being loaded is of type <tt>TestObject</tt>,            the OQL query will load <tt>FROM TestObject</tt>, whether the actual table name in            the database is <tt>test</tt>, <tt>test_object</tt>, or any other name. If a join is            required to load related objects, Castor will automatically perform the join.</span></p>          <p><span class="bodyGrey">The following code snippet uses an OQL query to load all the objects in a given group.            Note that product and group are related objects, the JDBC query involves a join:            <span class="bodyBlack"><pre>
OQLQuery     oql;
QueryResults results;

<span class="bodyGrey"><font color="red">// Construct a new query and bind its parameters</font></span>
oql = db.getOQLQuery( &quot;SELECT p FROM Product p WHERE Group=$1&quot; );
oql.bind( groupId );
<span class="bodyGrey"><font color="red">// Retrieve results and print each one</font></span>
results = oql.execute();
while ( results.hasMore() ) {
  System.out.println( results.next() );
}
           </pre></span></span></p>          <p><span class="bodyGrey">The following code snippet uses the previous query to obtain products, mark down            their price by 25%, and store them back to the database (in this case using            a client application transaction):             <span class="bodyBlack"><pre>
while ( results.hasMore() ) {
  Product prod;

  prod = (Product) results.next();
  prod.markDown( 0.25 );
  prod.setOnSale( true );
}
<span class="bodyGrey"><font color="red">// Explicitly commit transaction</font></span>
db.commit();
db.close();
           </pre></span></span></p>          <p><span class="bodyGrey">As illustrated above, a query is executed in three steps. First a query object is            created from the database using an OQL statement. If there are any parameters, the            second step involves binding these paramaters. Parameters are bound in the same            order as they appear in the OQL statement. The third step involves executing the            query and obtaining a result set of type <a href="api/org/exolab/castor/jdo/QueryResults.html"><api>org.exolab.castor.jdo.QueryResults</api></a>.</span></p>           <p><span class="bodyGrey">A query can be created once and executed multiple times. Each time it is executed            the bound parameters are lost, and must be supplied a second time. The result of            a query can be used while the query is being executed a second time.</span></p>          <p><span class="bodyGrey">The is also a special form of query that gives a possibility to call stored            procedures:            <span class="bodyBlack"><pre>
oql = db.getOQLQuery( &quot;CALL sp_something($) AS myapp.Product&quot; );
           </pre></span>            Here sp_something is a stored procedure returning one or more            ResultSets with the same sequence of fields as Castor-generated            SELECT for the OQL query &quot;SELECT p FROM myapp.Product p&quot;            (for objects without relations the sequence is: identity, then all            other fields in the same order as in mapping.xml).</span></p>                <a name="Create/remove/update"><h3>Create/remove/update</h3></a>          <p><span class="bodyGrey">The method <a href="api/org/exolab/castor/jdo/Database.html#create(java.lang.Object)">create(java.lang.Object)</a>            creates a new object in the database, or in JDO terminology makes a transient            object persistent. An object created with the <tt>create</tt> method will remain in            the database if the transaction commits; if the transaction rolls back the object            will be removed from the database. An exception is thrown if an object with the            same identity already exists in the database.</span></p>          <p><span class="bodyGrey">The following code snippet creates a new product with a group that was previously            queried:            <span class="bodyBlack"><pre>
Product prod;

<span class="bodyGrey"><font color="red">// Create the Product object</font></span>
prod = new Product();
prod.setSku( 5678 );
prod.setName( &quot;Plastic Chair&quot; );
prod.setPrice( 55.0 );
prod.setGroup( furnitures );
<span class="bodyGrey"><font color="red">// Make is persistent</font></span>
db.create( prod );
           </pre></span></span></p>          <p><span class="bodyGrey">The method <a href="api/org/exolab/castor/jdo/Database.html#remove(java.lang.Object)">remove(java.lang.Object)</a>            has the reverse affect, deleting a persistent object. Once removed the object is no            longer visible to any transaction. If the transaction commits, the object will be removed            from the database, however, if the transaction rolls back the object will remain in            the database. An exception is thrown when attempting to remove an object that is not            persistent.</span></p>              <a name="Using-JDO-And-XML"><h2>Using JDO And XML</h2></a>        <p><span class="bodyGrey">Castor JDO and Castor XML can be combined to perform transactional database operations          that use XML as the form of input and output. The following code snippet uses a combination          of persistent and transient objects to describe a financial operation.</span></p>        <p><span class="bodyGrey">This example retrieves two account objects and moves an amount from one account to the          other. The transfer is described using a transient object (i.e. no record in the database),          which is then used to generate an XML document describing the transfer. An extra step          (not shown here), uses XSLT to transform the XML document into an HTML page.</span></p>        <span class="bodyBlack"><pre>
Transfer tran;
Account  from;
Account  to;
OQLQuery oql;

tran = new Transfer();
<span class="bodyGrey"><font color="red">// Construct a query and load the two accounts</font></span>
oql = db.getOQLQuery( &quot;SELECT a FROM Account a WHERE Id=$&quot; );
oql.bind( fromId );
from = oql.execute().nextElement();
oql.bind( toId );
to = oql.execute().nextElement();

<span class="bodyGrey"><font color="red">// Move money from one account to the other</font></span>
if ( from.getBalance() &gt;= amount ) {
  from.decBalance( amount );
  to.incBalance( amount );
  trans.setStatus( Transfer.COMPLETE );
  trans.setAccount( from );
  trans.setAmount( amount );
} else {
  <span class="bodyGrey"><font color="red">// Report an overdraft</font></span>
  trans.setStatus( Transfer.OVERDRAFT );
}

<span class="bodyGrey"><font color="red">// Produce an XML describing the transfer</font></span>
Marshaller.marshal( trans, outputStream );
      </pre></span>        <p><span class="bodyGrey">The XML produced by the above code might look like:         <span class="bodyBlack"><pre>
&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;report&gt;
  &lt;status&gt;Completed&lt;/status&gt;
  &lt;account id=&quot;1234-5678-90&quot; balance=&quot;50&quot;/&gt;
  &lt;transfer amount=&quot;49.99&quot;/&gt;
&lt;/report&gt;
</pre></span></span></p>      </td></tr><tr height="5"><td width="10" height="5" bgcolor="#7270c2" valign="top" align="left">&nbsp;</td><td width="115" height="5" bgcolor="#7270c2" valign="top"><img src="images/dotTrans.gif" width="1" height="15" border="0"><br><img src="images/line_sm.gif" width="105" height="3" border="0" align="right"></td><td width="7" height="5" bgcolor="#a9a5de" valign="top" align="left">&nbsp;</td><td width="70" height="5" valign="top" align="left">&nbsp;</td><td width="120" height="5" valign="top" align="left">&nbsp;</td></tr><tr><td width="10" height="5" bgcolor="#7270c2" valign="top" align="left">&nbsp;</td><td width="115" bgcolor="#7270c2" valign="top" align="left"></td><td width="7" bgcolor="#a9a5de" valign="top" align="left"><img src="images/dotTrans.gif" width="1" height="25" border="0"></td><td width="70" valign="top" align="left"><img src="images/dotTrans.gif" width="1" height="25" border="0"></td><td width="120" valign="top" align="left">&nbsp;</td></tr><tr height="5"><td width="10" rowspan="2" height="100%" bgcolor="#7270c2" valign="bottom" align="left"><img src="images/stripes1.gif" width="10" height="125" border="0"></td><td width="115" rowspan="2" height="100%" bgcolor="#7270c2" valign="bottom" align="left"><img src="images/stripe105.gif" width="105" height="125" border="0"></td><td width="7" rowspan="2" height="100%" bgcolor="#a9a5de" valign="top" align="left">&nbsp;</td><td width="70" height="100%" valign="top" align="left">&nbsp;</td><td width="120" height="100%" valign="top" align="left">&nbsp;</td></tr><tr height="5"><td width="70" height="25" valign="top" align="left">&nbsp;</td><td width="400" height="25" valign="bottom" align="left"><br><br><img src="images/line_light.gif" border="0" width="400" height="3"><br><p></p><span class="bodyGrey"><small><notice>     Copyright ) 1999-2003 <a href="http://www.exolab.org">ExoLab Group</a>.  All rights reserved.   </notice><br>&nbsp;<br></small><small><notice>     Java, EJB, JDBC, JNDI, JTA, Sun, Sun Microsystems are trademarks or registered     trademarks of Sun Microsystems, Inc. in the United States and in other     countries. XML, XML Schema, XSLT and related standards are trademarks or registered     trademarks of MIT, INRIA, Keio or others, and a product of the World Wide Web     Consortium. All other product names mentioned herein are trademarks of their respective     owners.   </notice><br>&nbsp;<br></small></span><p></p>&nbsp;</td><td width="120" height="25" valign="top" align="left">&nbsp;</td></tr></table></body></html>