JDO UNPLUGGED - PART 1

Java Data Objects is an 'Object Relational Mapping' Technology developed by Java Community Process(JCP), with active support from Sun MicroSystems. Craig Russell is the specification lead for the JDO expert group and David Jordan is an active member of th

JDO UNPLUGGED - PART 1

JDO UNPLUGGED - PART I
----------------
by Farihah Noushene B.E.
=======================
(published in Developer IQ ? Oct 2005)

     


Java Data Objects is an 'Object Relational Mapping' Technology developed by Java Community Process(JCP), with active support from Sun MicroSystems. Craig Russell is the specification lead for the JDO expert group and David Jordan is an active member of that group.

Before the advent of JDO, we had various sun's official standards for storing data like JDBC and EJB with CMP and BMP. Also there are a number of ORM tools like Hibernate from SourceForge, Object Relational Bridge(OJB) from Apache, Toplink from Oracle etc., In this article we shall the unique features of JDO and how it differs from the rest of the technologies.

When we use JDBC, we have to manage the values of the fields explicitly and map them into the relational database tables. The developers have to write their SQL code. Also they have to implement their own mapping between the relational data model and their java object model which is very complex. Hence the benefits of Object-Oriented development are lost.

In CMP we can creates new objects, modifiy them, delete them and view them in memory. The programmer need not write any sql code for saving these objects in corresponding database. This is automatically done by the Container itself. CMP supports both object database and relational database but CMP cannot be used for complex applications. Enterprise Java Beans or EJB is developed to support distributed object computing. Due to the distributed capabilities, EJB applications become more complex. When our application needs object persistence, but does not need distributed object capabilities we can go for JDO instead of EJB's CMP or BMP. JDO can run in application server environment also and so the Session beans can be used along with JDO. Thus providing flexibility to choose the appropriate environment for our application. Also JDO fully exploits the object-oriented capabilities of Java. OOPs techniques like Inheritence, polymorphism can be used in JDO which is not possible in other technologies like JDBC and EJB architecture.

Unlike the ORM tools like hibernate which support only relational database, JDO supports a wide variety of data sources, including the data sources that are not commonly considered as databases. JDO supports Object databases and XML databases also. This is the major advantage of JDO over the other ORM tools. Thus JDO can be thought of as a futuristic approach.

JDO is an interface-based definition of object persistence and describes the storage, querying and retrieval of objects from the data stores. The collection of all class definitions that form an application is called application's 'object model'. These objects represent the state and behavior of the application. The storage of these objects beyond the lifetime of java virtual machine is called 'object-persistence'. Object Database Management System are the storage environment for Objects. ODBMS suffers from various factors such as inefficiencies in query capability, lack of well defined standards to invoke the persistent services, lock-in to vendors product etc.,

JDO introduces a new step in the deployment process called 'Class Enhancement'. Each persitent class must be enhanced so that it can be used in JDO runtime environment. The persistent classes are compiled using our traditional java compiler to produce a class files. The enhancer program reads these class files and JDO metadata to create new 'Enhanced Class Files'. Also the enhanced class files are binary compatible and so they can be used with any JDO implementation. The enhanced class files are also independent of any specific datastore. The enhancement process does not alter the funtional behavior of the class. It adds code to ensure that all the values of the field can be read from the datastore and modifications are tracked.

Also JDO supports 'Persistence-by-reachability' (as in Hibernate). That is, JDO causes non-persistent instance of a persistence class to become persistent at commit if it is reachable by a persistence instance directly or indirectly. ie., if we have two persistent capable classes that are related to each other, when we modify one of the class's instance, it will be updated in the other table also.

JDO provides a query language called JDO Query Language (JDOQL) to access instance based on specific search criteria. The JDOQL is can be used in any type of database like Relational Database, Object Database, Hierarchial database etc., The JDO queries is performed by 'Query' interface is used to select the instances that meet the specified criteria. The instance of 'Query' interface is created by using 'PersistenceManager' interface. The JDO Query allows us to filter out instances from set of instances specified by an Extent or a Collection. A Filter consists of a Boolean expression and it is applied to the instances. The query result includes all the instances for which the boolean expressions are true.
The syntax is
Extent extent = manager.getExtent (player.class,true);
String filter = "name == a ";
Query query = manager.newQuery(extent,filter);

In filter we can use a number of operators like equality operator(==), inequality operator(!=), comparison operators like greater than, greater than or equal to (<, >, <=, >=), Boolean operators like conditional AND, logical AND (&, &&, |, ||, !) and arithmetic operators like addition, subtration, multiplication, division (+, -, *, %, ~). The JDOQL also supports string expressions inside the filter. For this purpose startsWith(..) and endsWith() methods are provided. For example, we can write the filter as name.startsWith("a%"). It is similar to our sql command where we give "where name like 'a%'".

The order the query result to be displayed is specified by an ordering statement. The ordering statement is a string that contains one or more ordering declarations which consists of a java expression of orderable type followed by either 'ascending' or 'descending'. The syntax is
query.setOrdering("player.name ascending" +
    "player.game descending");
Finally the guery is closed by the 'close(..)' or closeAll() method. The 'close()' method closes the result returned by one call to 'query.execute()' method. The 'closeAll()' closes all the results from calls to 'query.execute()' method.

To quote from a recent article by Bruce Tate,
" Hibernate does not yet have the enterprise extensions or mapping flexibility of the best Java Data Object (JDO) solutions.".
With this brief introduction, we will see how to actually use JDO , in the next part of this tutorial.

==============================================================================

BOOKS FOR REFERENCE:
1. Java Data Objects (May 2003)
   - David Jordan and Craig Russell
  SPD Oreilly Publication.

2. Java Data Objects (2003)
   - Robin M. Roos. Addison-Wesley Publication.
This book can be downloaded free from the Net. To download the book, become a member of yahoo group 'Java Data Objects' and goto the main window of the group and download the book from files present there.