Page 4 of 6
The mapping module reads and parses the XML-based mapping file, and then provides information to Castor JDO for automatic translation between database data and Java objects.
Loading a mapping file is simple:
mapping = new Mapping(this.getClass().getClassLoader());
mapping.loadMapping("mapping.xml");
Castor JDO OQL is a subset of OQL, but not compatible with Sun JDO OQL. Like the Sun JDO OQL, Castor JDO OQL resembles SQL, except that it performs operations directly on Java objects instead of database tables, making the language more appropriate for use within a Java-based application.
The public interface org.exolab.castor.jdo.Query is also simple:
public void bind( Object value )
public QueryResults execute()
Note: The Query interface actually contains many bind(...) methods overloaded to allow for different argument types to be bound. Please consult Castor JDO documentation for details.
Below, you will find some JDO OQL query examples:
OQLQuery query = db.getOQLQuery("SELECT p FROM Person p WHERE name LIKE AND dob > AND married=");
OQLQuery query2 = db.getOQLQuery("select a from com.foo.Login a where to_lower(loginname) like order by lastname, firstname");
OQLQuery query3 = db.getOQLQuery( "SELECT c FROM Course c WHERE categories = AND program = AND status = " );
More advanced query features are still under development -- consult the documentation for more information.
Castor JDO supports many relational databases. The list includes the usual suspects, such as Oracle and SQL Server, but also open source data stores, such as MySQL, InterBase, and SAP DB. Castor JDO only supports relational databases.
Castor JDO compares to the ideal persistence layer quite favorably. It is elegant in its simplicity, nonintrusiveness, and transparency -- although its transparency is limited to relational data stores. The API is concise and well thought out.
As an open source project, Castor JDO is free and modifiable. But along with open source benefits come its drawbacks: limited support and lack of tutorials, or nonskeletal documentation for that matter. Lack of support and documentation can add hours to your first Castor project. Open or not, single implementation and limited support is not a good combination. You might stumble upon an issue currently not handled by Castor, grinding your project to a halt. With multiple implementations, you could look at alternative JDO products. This issue, though, is not Castor-specific; it is common with all open source software.
However, from a purely technical viewpoint, Castor is well thought out and developer friendly. It will remain popular with developers for a long time.
Let's now compare JDO with JDBC (Java Database Connectivity). Look at some sample code written using Castor JDO running in
a J2EE environment (the session bean method sets transaction boundaries). The following code creates a new Incident instance, adds three children objects of class EventLogItem, and stores the object and its children in a database: