home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-04-23 | 3.7 KB | 135 lines |
- package COM.odi.demo.people;
-
- /**
- * <H3>Copyright (C) Object Design Inc. 1996, 1997</H3>
- *
- * This is the source code of the basic example used in the
- * documentation of ObjectStore Java Pro.
- */
-
- // Import the COM.odi package, which contains the ObjectStore Java 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[]) {
- String dbName = argv[0];
-
- // The following line initializes the ObjectStore Java software.
- // Currently, PSE and PSE Pro ignore the arguments.
-
- ObjectStore.initialize(null, null);
- try {
- Database.open(dbName, ObjectStore.OPEN_UPDATE).destroy();
- } catch (DatabaseNotFoundException e) {
- }
- Database db = createDatabase(dbName);
- readDatabase(db);
- }
-
- static Database createDatabase(String dbName) {
-
- // Call the Database.create() method to create and open the
- // database that is specified on the command line
- // when the application is invoked:
-
- Database db;
-
- try {
- db = Database.open(dbName, ObjectStore.OPEN_UPDATE);
- db.destroy();
- } catch (DatabaseNotFoundException e) {
- }
-
- 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 persistent-capable object.
- // ObjectStore Java 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, into 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 to also reach sophie and joseph.
- // In each transaction, an application must obtain a database root
- // and use it to navigate to persistent objects. Be sure to write
- // your application so that it does not hold on to references
- // to persistent objects between transactions.
-
- 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 ends the
- // accessibility of the persistent objects and abandons
- // the transient objects.
-
- 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.
- public int hashCode() {
- return super.hashCode();
- }
-
- }
-
-