ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button
Java Data Objects

JDO or CMP?

by David Jordan and Craig Russell, authors of Java Data Objects
05/21/2003

Author's note: Java Data Objects (JDO) and Enterprise JavaBeans (EJB) Container Managed Persistence (CMP) were developed concurrently, but took different paths.

JDO's design center is making Plain Old Java Objects (POJOs) persistent in any tier of an enterprise architecture, while CMP's solution is container-bound with elaborate requirements for development and deployment.

The similarities and differences between these technologies has been the subject of debate since the specifications were published. For example, see the discussions at JDOCentral.com. There are presentations comparing these approaches coming up at the JavaOne 2003 conference in San Francisco, California, June 10-13.

Technical session 3368 examines the integration possiblities and best practices with JDO and Struts, an open source Web application framework for JSP/Servlet containers. BOF 3236 discusses integrations with JDO and EJB containers. BOF 1289 contrasts design patterns for application development with JDO, JDBC, and EJB.

Here's a brief excerpt from Chapter 17 of our book, Java Data Objects, which summarizes the key trade-offs between using JDO and CMP for your enterprise application. --Craig Russell

JDO or CMP?

Both CMP beans and JDO persistent classes have features that you should consider before committing your project to use either strategy.

JDO persistent classes are suitable for modeling both coarse-grained and fine-grained persistent instances and in an application server are typically used behind session beans. CMP beans are typically used behind session beans; their remote behavior is seldom exploited.

JDO persistent classes can be used without recompilation in any tier of a distributed architecture and can be debugged in a one- or two-tier environment prior to integration into a web or application server. CMP beans can be debugged only after deployment into the application server.

Unlike servlets, JSP pages, and EJB components, there is no built-in remote behavior with JDO classes. All of the distributed, transaction, and security policies are based on the single persistence manager that manages all of the persistent instances of your model. This means that JDO persistent classes can be used in any tier of a distributed application and remote behavior is implemented by the container, not the JDO implementation.

CMP beans give you a high degree of portability across application servers. The bean class and required deployment descriptor are standard. Most of the incompatibilities between implementations are found in unspecified areas of mapping beans to the underlying datastore, optional features such as read-only beans, and extensions in deployment and management of beans. JDO implementations vary with regard to the optional features that they support.

Related Reading

Java Data Objects
By David Jordan, Craig Russell

With CMP, you identify every bean class, persistent field, and persistent relationship in the deployment descriptor. Using JDO, you identify every persistent class in the metadata, but you can usually take the default for the persistence of fields, including relationships.

With CMP, relationships are managed; this means that during the transaction a change to one side of the relationship immediately affects the other side, and the change is visible to the application. JDO does not support managed relationships, although some vendors offer them as optional features.

Inheritance is a common paradigm for modeling real-world data, but CMP beans do not support inheritance. CMP makes a distinction between the implementation class and the bean. The abstract bean-implementation classes and the local and remote interfaces can form inheritance relationships, but the CMP beans that model the application's persistent classes cannot. Relationships in CMP are between CMP beans, not implementation classes, and these relationships cannot be polymorphic. In our example, it would be impossible for a MediaItem CMP bean to have a relationship with a MediaContent CMP bean, because MediaContent has no instances. In order to implement this kind of model, you would need to change the MediaItem CMP bean to have two different relationships: one between MediaItem and Movie, and another between MediaItem and Game. You would need to treat the relationships separately in every aspect of the bean.

The programming model used to access fields is very different between CMP beans and JDO. With CMP beans, all persistent fields and relationships are defined by abstract get and set methods in the abstract bean class, plus a declaration in the deployment descriptor. Access to the field value is the responsibility of the concrete implementation class generated by the CMP code-generation tool. With JDO, persistent fields and relationships are declared or defaulted in the metadata, and access to the field values is provided by the code in the class for transient instances or by the JDO implementation for persistent instances. The JDO enhancer generates the appropriate field-access code during the enhancement process.

JDOQL and EJBQL provide similar access to data in the datastore. Both allow you to select persistent instances from the datastore to use in your programs. Both use the read-modify-write pattern for updating persistent data. Neither language is a complete data-manipulation language; both are used only to select instances for manipulation by the programming language.

CMP beans require active transactions for all business methods. Nontransactional access is not standard or portable. JDO allows you to choose whether transactions are required. JDO requires inserts, deletes, and updates to be performed within transactions, but read-only applications, including caching, can be implemented portably without transactions.

Table 17-1 is a summary comparing CMP beans with JDO persistent classes.

Table 17-1: Comparison of CMP beans and JDO


Characteristic

CMP beans

JDO persistent classes

Environmental

Portability of applications

Few portability unknowns

Documented portability rules

Operating environment

Application server

One-tier, two-tier, web server, application server

Independence of persistent classes from environment

Low: beans must implement EJB interfaces and execute in server container

High: persistent classes are usable with no special interface requirements and execute in many environments

Metadata

Mark persistent classes

Deployment descriptor identifies all persistent classes

Metadata identifies all persistent classes

Mark persistent fields

Deployment descriptor identifies all persistent fields and relationships

Metadata defaults persistent fields and relationships

Modeling

Domain-class modeling object

CMP bean (abstract schema)

Persistent class

Inheritance of domain-class modeling objects

Not supported

Fully supported

Field access

Abstract get/set methods

Any valid field access, including get/set methods

Collection, Set

Supported

Supported

List, Array, Map

Not supported

Optional features

Relationships

Expressed as references to CMP local interfaces

Expressed as references to JDO persistent classes or interfaces

Polymorphic references

Not supported

Supported

Programming

Query language

EJBQL modeled after SQL

JDOQL modeled after Java Boolean expressions

Remote method invocation

Supported

Not supported

Required lifecycle methods

setEntityContext, unsetEntityContext, ejbActivate, ejbPassivate, ejbLoad, ejbStore, ejbRemove

no-arg constructor (may be private)

Optional lifecycle callback methods

ejbCreate, ejbPostCreate, ejbFind

jdoPostLoad, jdoPreStore, jdoPreClear, jdoPreDelete

Mapping to relational datastores

Vendor-specific

Vendor-specific

Method security policy

Supported

Not supported

Method transaction policy

Supported

Not supported

Nontransactional access

Not standard

Supported

Required classes/interfaces

EJBLocalHome, local interface (if local interface supported);

EJBHome, remote interface (if remote interface supported);

Abstract beans must implement EJBEntityBean;

Identity class (if nonprimitive identity)

Persistent class;

objectid class (only for application identity)

Transaction synchronization callbacks

Not supported

Supported

David Jordan founded Object Identity, Inc. to provide Java Data Objects (JDO) consulting and training services. David is also a coauthor of O'Reilly's book on Java Data Objects, with Craig Russell.

Craig Russell is the specification lead for JDO at Sun Microsystems.


O'Reilly & Associates recently released (April 2003) Java Data Objects.


Return to ONJava.com.