This chapter discusses the following topics:
Overview of Required Components
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
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(); } }
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\osjiOn 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
javac *.javaAs output, the javac compiler produces the byte code class file Person.class.
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.classOn UNIX, to run the postprocessor, enter
osjcfp -dest . -inplace Person.classThe -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
java COM.odi.demo.people.Person person.odbThe 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.
Updated: 10/07/98 08:44:55