Chapter 12. Object-Relational Mapping and Java Data Objects

The major premise of this book is that when we design an application in Java, we often find that we need to rely on a relational database in order to store our data. In fact, the majority of business applications are nothing more than a user interface for maintaining data stored in a database.

Throughout this book, we've explored a number of approaches to the problem of using data in the object-oriented environment of Java that we retrieve and store in a relational database.

At one end of the spectrum of approaches is direct database access from Java classes, where we embed relational queries using SQL in our Java code, and map the data to Java objects on an ad-hoc basis (or consume the data in place).

At the other end of this spectrum are Oracle's object-relational features where we create objects in the database and import them into Java, either as weakly typed Structs (or STRUCT) or strongly typed custom classes, using the SQLData (or ORAData) interface.

Another approach and one that avoids these extremes is to introduce an intermediary between the object realm of Java and the relational regime of Oracle. We saw how this can be done for simple cases using container-managed persistence in EJB. But EJBs come with a lot of baggage that is a major burden (in terms of development, performance, and cost) if you don't need distributed components and the other services EJB provides.

In this chapter, we'll examine an example of an object-relational mapping tool designed explicitly for bridging the gap between Java and relational databases. O-R mapping tools go well beyond a simple one-object-to-one-row mapping. With little effort from the developer, these tools easily support complex object models involving inheritance and composition in one-to-one, one-to-many, and many-to-many relationships.

TopLink, for example, is a mature object-relational product that was originally developed in the early 1990s for the Smalltalk programming language before being rewritten and released for Java in 1996.

The main TopLink program, MappingWorkbench, is a graphical user interface that allows users to map tables to classes. TopLink can use either existing classes or tables, or it can create them. Unlike the simple O-R mapping in JDeveloper (at least the Oracle 9i Release 2 version) which allows only simple, one-to-one mapping between a table and a CMP entity bean, TopLink allows us to model complex relationships between our Java objects, including one-to-many and many-to-many relationships, and to map them to database tables.

Once mappings are established, TopLink can export descriptors that are used to generate wrappers for an application's objects; these wrappers completely hide all details regarding the database from the application and let the developer concentrate entirely on the object model.

TopLink was acquired by Oracle in June 2002, probably with the intention of integrating it with its existing products. We can expect, for example, to see some of its technology used to provide more sophisticated mapping for EJBs in JDeveloper. TopLink features are also likely to appear in Oracle's Business Components for Java framework, as well.

An alternative to TopLink is a relatively new Java technology, Java Data Objects (JDO). As mentioned previously, within six months of the release of the JDO 1.0 specification in April 2002, at least nine commercial JDO implementations were available and several open-source projects were underway.

Every indication is that JDO will be increasingly supported in both new and existing products. (TopLink has limited support for JDO that was introduced before it was purchased by Oracle but it is unclear whether Oracle will continue to develop these features.) The rest of this chapter will be limited to covering JDO but many of the principles are applicable to TopLink and other proprietary object-relational mapping tools.



Java Oracle Database Development
Java Oracle Database Development
ISBN: 0130462187
EAN: 2147483647
Year: 2002
Pages: 71

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net