JDO UNPLUGGED - PART I
Posted On October 22, 2005 by Sneha Latha filed under Java
Java Data Object (JDO) 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 unravel the unique features of JDO and how it differs from the rest.
When we use JDBC, we have to manage the values of the fields explicitly and map them into the relational database tables. 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. As such, the benefits of Object-Oriented development are lost.
In CMP, we can create new objects, modify them, delete them and view them in memory. A programmer need not write any SQL code for saving these objects in the corresponding database. The Container itself automatically does this. CMP supports both object databases and relational databases. However, CMP cannot be used for complex applications. Enterprise Java Beans or EJB is developed to support distributed object computing. Owing 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 in for JDO instead of EJB's CMP or BMP. JDO can run in an application server environment also and so the Session beans can be used along with JDO. Thus, it provides flexibility to choose the appropriate environment for our application. Also, JDO fully exploits the object-oriented capabilities of Java. OOPs techniques like Inheritance and polymorphism can be used in JDO, which is not possible in other technologies like JDBC and EJB architecture.
Unlike 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 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 datastores. The collection of all class definitions that form an application is called the application's 'object model'. These objects represent the state and behavior of the application. Storage of these objects beyond the lifetime of java virtual machine is called 'object-persistence'. Object Database Management Systems (ODBMS) 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 the vendor’s product, etc.
JDO introduces a new step in the deployment process called 'Class Enhancement'. Each persistent class must be enhanced so that it can be used in JDO runtime environment. Persistent classes are compiled using our traditional java compiler to produce class files. The enhancer program reads these class files and JDO metadata to create new 'Enhanced Class Files'. Moreover, the enhanced class files are binary compatible and can thus be used with any JDO implementation. Enhanced class files are also independent of any specific datastore. The enhancement process does not alter the functional 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.
JDO even 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. Let me elaborate. If we have two persistent capable classes that are related to each other, when we modify one of the class' 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. JDOQL can be used in any type of database like Relational Database, Object Database, Hierarchical database, etc. JDO queries performed by 'Query' interface are used to select the instances that meet the specified criteria. The instance of 'Query' interface is created using 'PersistenceManager' interface. The JDO Query allows us to filter out instances from a 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, subtraction, multiplication, division (+, -, *, %, ~). 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 of 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 go to the main window of the group and download the book from files present there.
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 unravel the unique features of JDO and how it differs from the rest.
When we use JDBC, we have to manage the values of the fields explicitly and map them into the relational database tables. 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. As such, the benefits of Object-Oriented development are lost.
In CMP, we can create new objects, modify them, delete them and view them in memory. A programmer need not write any SQL code for saving these objects in the corresponding database. The Container itself automatically does this. CMP supports both object databases and relational databases. However, CMP cannot be used for complex applications. Enterprise Java Beans or EJB is developed to support distributed object computing. Owing 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 in for JDO instead of EJB's CMP or BMP. JDO can run in an application server environment also and so the Session beans can be used along with JDO. Thus, it provides flexibility to choose the appropriate environment for our application. Also, JDO fully exploits the object-oriented capabilities of Java. OOPs techniques like Inheritance and polymorphism can be used in JDO, which is not possible in other technologies like JDBC and EJB architecture.
Unlike 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 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 datastores. The collection of all class definitions that form an application is called the application's 'object model'. These objects represent the state and behavior of the application. Storage of these objects beyond the lifetime of java virtual machine is called 'object-persistence'. Object Database Management Systems (ODBMS) 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 the vendor’s product, etc.
JDO introduces a new step in the deployment process called 'Class Enhancement'. Each persistent class must be enhanced so that it can be used in JDO runtime environment. Persistent classes are compiled using our traditional java compiler to produce class files. The enhancer program reads these class files and JDO metadata to create new 'Enhanced Class Files'. Moreover, the enhanced class files are binary compatible and can thus be used with any JDO implementation. Enhanced class files are also independent of any specific datastore. The enhancement process does not alter the functional 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.
JDO even 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. Let me elaborate. If we have two persistent capable classes that are related to each other, when we modify one of the class' 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. JDOQL can be used in any type of database like Relational Database, Object Database, Hierarchical database, etc. JDO queries performed by 'Query' interface are used to select the instances that meet the specified criteria. The instance of 'Query' interface is created using 'PersistenceManager' interface. The JDO Query allows us to filter out instances from a 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, subtraction, multiplication, division (+, -, *, %, ~). 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 of 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 go to the main window of the group and download the book from files present there.
