|
Performing Updates
Updates are very easy with an OODB. The object already exists in the database, so it is simply a matter of retrieving the object and then calling its mutator methods. You can also set the object's public attributes directly if you wish. Once you commit the transaction, the changes are applied transparently to the object in the database.
Code: Updating existing PhonebookEntry Objects
Transaction t = Transaction.begin(ObjectStore.UPDATE);
PhonebookEntry entry = (PhonebookEntry) entries.get("555-123-4567);
entry.setFirstName("Steve"); }
t.commit(ObjectStore.RETAIN_HOLLOW);
In the preceding example, the code retrieves an object from the hash map using a phone number as the key. More code is usually required to handle a null return due to a non-existent object. Once the object is retrieved, it can be manipulated as a normal Java object. Once all changes have been made to the object, the transaction is committed and changes are applied to the database.
See the Code in Full
By adding only 130 statement lines of code to the application, I was able to add query, update, delete, and reporting functionality with ObjectStore PSE. A brief transcript shows the new application in action.
At the beginning of this article, I mentioned that I started out writing an application with very little consideration of the final storage approach. You can view the final code in the following files:
Red sections are the pieces of code that I added while migrating from version 1 of the application to the OODB-enabled application. Some areas that I changed to OODB-enable the application are marked by comments (beginning with "Changed:" or "Added:") explaining the modifications.
You can download the source code for the application in case you would like to try building it for yourself. Be warned though, the code is partial and buggy, and it lacks error checking. (If you receive "Out of Environment Space" errors from older versions of Windows, refer to the README.TXT file.) THe code is intended only to show basic OODB conceptsreuse pieces at your own risk.
In a Nutshell
Our phone book app demonstrates some basic features of an ObjectStore PSE OODB. Each OODB is somewhat different of course, but many of the concepts in this article apply to other products. You can easily rebuild and tailor this demonstration to your own learning purposes. OODBs are not a silver bullet, and I hope I have not given that impression. They have some important limitations that you should be aware of (see "Object-Oriented Databases Are Worth a Closer Look").
In addition to this article, you also should study the whitepapers and documentation supplied with ObjectStore PSE. They address a number of general concepts and specific examples that were very useful when I first worked with ObjectStore PSE and in developing this article.
Steve Franklin handles the architecture and project engineering responsibilities at a major software firm dealing with J2EE, client/server, command and control, and other distributed architectures. Steve Franklin's primary "off-hours" hobby can be found at Lookoff.com, a repository for Internet and research resources. He can be reached at steve@lookoff.com.
|