10.3. Mapping Databases to Objects with OJB
Using a pool can make a program or Web site run much faster, but it does nothing to make development any easier. As useful and powerful as JDBC is, the fact remains that there is a mismatch between databases and Java. Databases and JDBC deal with entities like rows and columns and metadata, whereas Java programs deal with classes and objects. If databases could store objects, then the task of making JDBC calls could be reduced to calling accessors in objects, which is much simpler and more familiar. This would also make it possible to use tools like BeanUtils and JXPath to traverse information from databases.There are some databases, called object databases that allow objects to be stored directly. However, these have never been as successful as relational databases, so what is needed is some way to bridge the gap between databases and objects. Such a tool is called an object-relational mapping tool, and Apache provides an excellent one called OJB.There are several steps to using OJB. First, the classes and underlying database must be defined. Then OJB must be configured to properly connect tables and classes. This is accomplished through a number of XML files. Finally, OJB provides a series of APIs that allow objects to be retrieved, stored, modified, and deleted. Each of these steps will be examined in turn.