The distributor had just bought out a few rival businesses in town. The paperwork had been signed, the funds were transferred and the deal was complete. The only detail to be worked out was to link all the locations together into one coordinated unit. The MIS manager was horrified to learn that the databases of the other locations are as diverse as can be. Some of them are relational databases, whereas the home database is of the newer object type. What he needs is a tool that allows him to "wrap" all the databases together into one manageable, tightly knit application. What he needs is TopLink from The Object People.
TopLink is a powerful new application that allows the developer to work at the object level, even if the database in question is relational. The objects are mapped to the database in a non-intrusive manner; therefore, the relational database can be made to behave much like an object database.
Why Not Just Change the Database from Relational to Object?
There are a few good reasons why companies are not willing to do this at this time. One is that relational database technology is regarded as the standard. Accessible through a wide range of applications, the relational database is the one most working professionals are familiar with. The object database, although it has come a long way in the past few years, is still regarded as new, unfamiliar and untested technology. Even if an organization wanted to make the change, the time and expense of re-encoding data and training personnel would scare them away from this option.
TopLink Provides the Perfect
Solution for this Dilemma
TopLink allows relational databases and objects to be "bridged" together in a cost-effective manner. If object database technology someday becomes the new standard (some think it will), TopLink can allow an easier migration to this change, saving a lot of time and expense in the process.
What is TopLink?
Basically, TopLink provides a flexible mechanism that allows the end user to store Java objects in relational database tables. It is a layer of Java code between a Java application and the relational database being used. It acts as an object-orientated "wrapper" around the relational database and allows "mapping" of objects into a series of relational tables. This wrapper protects the application from changes in the underlying database. Changes in the database should have no effect on the code in an application.
TopLink installation requires:
If your system has an up-and-running Sun JDK 1.1 or higher, it should meet these requirements.
- A JDBC driver which can connect with the local database system
- A version of Java which is compatible with JDBC API
Installation from the CD-ROM was basic and took only a few minutes. After installation is complete, it is necessary to perform a few steps:
- The local JDBC drivers must be tested - This is done by entering database login information into some code that is supplied with the installation. The code must then be compiled and executed. If all goes well, a message reading: "Successful Disconnection. Test Complete" will be displayed.
- Configure the local Java environment - Configure development environment parameters which are IDE specific.
- Set the appropriate CLASSPATH variable - I ran the application in WIN 95 using JDK 1.1.5, so my CLASSPATH was set to the following:
- Run a final installation test - This is done by compiling and running some more of the supplied code. A message that reads: "Connection successful" will indicate that all is well.
I found installation and configuration of TopLink to be smooth and uncomplicated. Now that the application is up and running, let's look at what it can do. I'll use the example supplied with the application to demonstrate some of the steps necessary to create a simple database application.
The sample included with the software is an employee management database. It keeps track of information for all full-time and contract employees, including addresses and phone numbers. The object model being built consists of three classes:
- The Employee class represents all employees, both full time and contracted. It includes all personal information, including references to home addresses and phone numbers.
- The Address class represents home address. This includes country, street, city and zip code.
- The Phone number class contains, as you may have guessed, the phone number including area code.
Each of the above classes contains cross-references to the others. The relational database stores all of this information in three tables, much in the same fashion that I've built the object model. It is important to point out here that it's not necessary to structure the object model after the table structure of the relational database. The object model can be based on application requirements rather than the database structure. This is a great feature, as the developer is not bound to the relational database structure.
TopLink communicates with a relational database via a Database Session. This Java class keeps track of the following information:
- Descriptors - Maintain the associations between tables and Java classes
- Identity Maps - Used to cache and maintain identity
- Database Accessor - Handles low level communication between the session and the relational database
TopLink takes the relational database, and allows it
to be treated as an object database
Logging into the Database
In order to login to a database, it is first necessary to create a JDBCLogin instance. This holds the login parameters including the database name and type. This class is then passed to the DatabaseSession, which allows the login to occur. Here's a small fragment of the code that allows the creation of a login instance:
Project project = TOPLink.Public.
Now that the database is logged into, it's time to discuss the initialization of the descriptors. This is done by the following steps:
All in all, it really hasn't been too difficult up to this point. There has been a little filling in the blanks of code, and then compiling to run, but most of the major jobs were done by clicking on menus. That's pretty amazing, when you consider just how potent an application this is. This fraction of an example only begins to scrape the surface of just how many uses there are for this powerful builder.
- Create a Project using the TopLink Builder tool. This is a stand-alone application used to create and manage the descriptors and mappings. It's GUI interfaced and looks like your typical drop-menu driven application. It obtains data from the database and stores it in Table files. Class Definition files generated from the persistent Java classes are supplied, and the Builder is then used to map the table columns to class attributes. The Builder then writes Descriptor files to these mappings. These are used by the Java application to access the database.
- If required, modify your persistent Java classes. It is required that all persistent classes have a public default constructor which has no arguments. If any of the class attributes are declared private, it is necessary to create accessor methods to allow them to be written to the database.
- Create Class Definition Files for each of the persistent Java classes. Each of the Java classes in the project must have a descriptor. Opening the Builder and selecting "Add/Update" classes from the Descriptor menu can create these.
- Use the Builder's Database Login screen to connect to the database. This is done by selecting "Login To The Database" from the Builder menu. A dialogue box will drop open, allowing database connection information to be entered.
- Import table information from the database to the project. This is also done through the builder. All that's involved here is selecting the "Add Existing" from the Tables menu.
It is also possible to develop three-tier applications using TopLink. It supports the following databases (and many others through JDBC drivers):
Mainframe connectivity is also available for legacy data.
- Microsoft SQL Server
TopLink is one of the more powerful database tools I've seen. It offers flexibility and relative user-friendliness for this type of application. For those of you looking for a powerful tool to develop Java applications by using relational databases, or for someone who wants a good tool to develop a three-tier environment, it is definitely a must see.
I remember when I first discovered Java. At first I though it was this neat little scripting language that jazzed up Web pages. I almost immediately discovered that I had underestimated its power. I watched that language, in a relatively short period of time, become a major competitor of C, the age-old deity of programming languages. Now, it seems Java is invading the database management and development platforms as well. Will Java take over these traditional applications, and become the dominant development environment of the next century? That remains to be seen, but applications like TopLink provide a good clue as to what just might happen.
About the Author
Edward Zebrowski is a technical writer based in the Orlando, FL area. Ed runs his own Web development company, ZebraWeb, and can be reached on the Net at [email protected]