Example program Personnel, shown in Figure 2, uses a slightly more complicated database to store information about people. Use the New button to create a new Person object. Fill in the values, and click Save to save the object into the database.
Figure 2 Example program Personnel displays and edits simple Person objects.
If you enter a last name and a first name, and click Fetch, the program retrieves that person's database entry and uses it to recreate the Person object. If you change the Person object's data values, you can click Save to update the Person object's database record. Click Close to close the current Person object.
This program provides some simple object-caching. If you try to fetch a Person object and that object is already loaded, the database routines return a new reference to the already loaded object. To see this, create a new record in the top group box and click Save. Then use the Fetch button to load the same Person in the bottom group box. Now make changes to the upper display and click Save again. If you click the bottom Refresh button, the program redisplays the bottom Person object's values. Note that it does not make another trip to the database; it simply copies the object's values into the text boxes. The changes you made to the upper copy of the Person object should be visible in the lower copy, showing that the two are using references to the same object.
Don't expect this to be completely idiot-proof. Things can get a bit confusing if you start changing the first and last names used to identify Person objects. This program just shows that it is possible to make an object database that allows only one copy of a given object at a time.
Typical applications get around problems like these by assuming some key (customer name, account number, Social Security number) never changes. That gives the program a fixed handle to work with.
Program Personnel uses an Index field as its fixed key. The database's People table stores each object's combined first and last names, the object's serialization, and an Index field. The database automatically generates the Index when it creates a new record. After that, the Index never changes, even if the first and last names are modified.