ObjectStore Java API User Guide
Chapter 2

Example of Using ObjectStore

This chapter provides a simple example of a complete ObjectStore program. The code for this example is in the COM\odi\demo\people directory provided with ObjectStore.

This chapter discusses the following topics:

Overview of Required Components

Sample Code

Before You Run the Program

Running the Program

Overview of Required Components

The sample program stores some information about a few people and then retrieves some of the information from the database and displays it. The program shows the components you must include in your application so that it can use ObjectStore. These components are

When you write an ObjectStore program, you write it as though classes are persistence-capable. However, a program cannot store objects persistently until you run the ObjectStore-provided class file postprocessor. The postprocessor generates annotated versions of the class files. The annotated version of the class definition is persistence-capable. You run the postprocessor after you compile the program and before you run the program.

Sample Code

package COM.odi.demo.people;
// Import the COM.odi package, which contains the API:
import COM.odi.*;
public
class Person {
      // Fields in the Person class:
      String name;
      int age;
      Person children[];
      // Main:
      public static void main(String argv[]) {
            try {
                  String dbName = argv[0];
                  // The following line starts a nonglobal session and joins this
                  // thread to the new session. This allows the thread to use
                  // ObjectStore. 
                  Session.create(null, null).join();
                  Database db = createDatabase(dbName);
                  readDatabase(db);
                  db.close();
            }
      // The following shuts down ObjectStore.
            finally {
            Session.getCurrent().terminate();
      }
      }
      static Database createDatabase(String dbName) {
            // Attempt to open and destroy the database specified on the
            // command line. This ensures that the program creates a
            // new database each time the application is called.
            try {
      Database.open(dbName, ObjectStore.OPEN_UPDATE).destroy();
            }      catch (DatabaseNotFoundException e) {
            }
            // Call the Database.create() method to create a new database.
      Database db = Database.create(dbName, 
      ObjectStore.ALL_READ | ObjectStore.ALL_WRITE); 
            // Start an update transaction:
            Transaction tr = Transaction.begin(ObjectStore.UPDATE);
            // Create instances of Person:
            Person sophie = new Person("Sophie", 5, null);
            Person joseph = new Person("Joseph", 1, null);
            Person children[] = {sophie, joseph};
            Person tim = new Person("Tim", 35, children);
            // Create a database root and associate it with 
            // tim, which is a persistence-capable object.
            // ObjectStore uses a database root as an entry
            // point into a database. 
            db.createRoot("Tim", tim);
            // End the transaction. This stores the three person objects,
            // along with the String objects representing their names, and
            // the array of children, in the database. 
            tr.commit();
            return db;
      }
      static void readDatabase(Database db) {
            // Start a read-only transaction:
            Transaction tr = Transaction.begin(ObjectStore.READONLY);
            // Use the "Tim" database root to access objects in the 
            // database. Because tim references sophie and joseph,
            // obtaining the "Tim" database root allows the program 
            // also to reach sophie and joseph.
            Person tim = (Person)db.getRoot("Tim");
            Person children[] = tim.getChildren();
            System.out.print("Tim is " + tim.getAge() + " and has " + 
                  children.length + " children named: ");
            for (int i=0; i < children.length; i++) {
                  String name = children[i].getName();
                  System.out.print(name + " ");
            }
            System.out.println("");
            // End the read-only transaction. 
            // This form of the commit method ends the accessibility
            // of the persistent objects and makes the objects stale.
            tr.commit();
      }
      // Constructor:
      public Person(String name, int age, Person children[]) {
             this.name = name; this.age = age; this.children = children;
      }
      public String getName() {return name;}
      public void setName(String name) {this.name = name;}
      public int getAge() {return age;}
      public void setAge(int age) {this.age = age;}
      public Person[] getChildren() {return children;}
      public void setChildren(Person children[]) {
                  this.children = children;
      }
      // This class is never used as a persistent hash key, so
      // include the following definition. If you do not, then 
      // when you run the postprocessor it is unclear whether or
      // not you intend to use the class as a hash code. 
      // Consequently, the postprocessor inserts a hashCode 
      // function for you. The following definition avoids this.
      public int hashCode() {
            return super.hashCode();
      }
}

Before You Run the Program

Before you can run the sample program, you must

Adding An Entry to CLASSPATH

In your CLASSPATH environment variable, you already have two entries related to ObjectStore:

Ensure that these zip files are explicitly in your class path. An entry for the directory that contains them is not sufficient.

Another entry is required for you to be able to build and run the program. This entry names the ObjectStore installation directory, and allows ObjectStore to locate the annotated class files when you run the program.

For example, on Windows, if you place the ObjectStore distribution in the c:\odi\osji directory, you need the following entries:

c:\odi\osji\osji.zip;c:\odi\osji\tools.zip;c:\odi\osji
On UNIX, if you place the ObjectStore distribution in /usr/local/osji, you need

/usr/local/osji/osji.zip:/usr/local/osji/tools.zip:/usr/local/osji

Compiling the Program

To compile the program, change to the COM\odi\demo\people directory and enter

javac *.java
As output, the javac compiler produces the byte code class file Person.class.

Running the Postprocessor

You must run the class file postprocessor to make the Person class persistence-capable. The postprocessor generates new annotated class files. After you run the postprocessor, your program uses the annotated class files and not the original class files.

Ensure that the bin directory that contains the osjcfp executable is in your path, as noted in the README file in the installation directory and the postprocessor documentation. See Preparing to Run the Postprocessor.

On Windows, to run the postprocessor, enter

osjcfp -dest . -inplace Person.class
On UNIX, to run the postprocessor, enter

osjcfp -dest . -inplace Person.class
The -dest option specifies a destination directory for the annotated files. It is a required option. The -inplace option specifes that the postprocessor should overwrite the original class files. When you specify the -inplace option, the postprocessor ignores the -dest option.

The result from the osjcfp command shown above is

The -inplace option is the best choice for this example. But when you are in an iterative development cycle, it is best not to specify -inplace. During development, putting the postprocessed files in a different directory avoids errors.

Running the Program

Run the program as a Java application. Here is a typical command line:

java COM.odi.demo.people.Person person.odb
The argument is the pathname of the database.

The expected output is

Tim is 35 and has 2 children named: Sophie Joseph
Also, the example application creates or replaces the person.odb database in the current directory.



[previous] [next]

Copyright © 1998 Object Design, Inc. All rights reserved.

Updated: 10/07/98 08:44:55