home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Extras / ODesign / SetupPSE.exe / data.z / Person.java < prev    next >
Encoding:
Java Source  |  1997-04-23  |  3.7 KB  |  135 lines

  1. package COM.odi.demo.people;
  2.  
  3. /**
  4.  *      <H3>Copyright (C) Object Design Inc. 1996, 1997</H3>
  5.  *
  6.  * This is the source code of the basic example used in the
  7.  * documentation of ObjectStore Java Pro.
  8.  */
  9.  
  10. // Import the COM.odi package, which contains the ObjectStore Java API:
  11. import COM.odi.*;
  12.  
  13. public
  14. class Person {
  15.  
  16.   // Fields in the Person class:
  17.  
  18.   String name;
  19.   int age;
  20.   Person children[];
  21.  
  22.   // Main:
  23.  
  24.   public static void main(String argv[]) {
  25.     String dbName = argv[0];
  26.  
  27.     // The following line initializes the ObjectStore Java software.
  28.     // Currently, PSE and PSE Pro ignore the arguments.
  29.  
  30.     ObjectStore.initialize(null, null);
  31.     try {
  32.       Database.open(dbName, ObjectStore.OPEN_UPDATE).destroy();
  33.     } catch (DatabaseNotFoundException e) {
  34.     }
  35.     Database db = createDatabase(dbName);
  36.     readDatabase(db);
  37.   }
  38.     
  39.   static Database createDatabase(String dbName) {
  40.  
  41.     // Call the Database.create() method to create and open the
  42.     // database that is specified on the command line
  43.     // when the application is invoked:
  44.  
  45.     Database db;
  46.  
  47.     try {
  48.       db = Database.open(dbName, ObjectStore.OPEN_UPDATE);
  49.       db.destroy();
  50.     } catch (DatabaseNotFoundException e) {
  51.     }
  52.  
  53.     db = Database.create(dbName,
  54.              ObjectStore.ALL_READ | ObjectStore.ALL_WRITE);
  55.  
  56.     // Start an update transaction:
  57.  
  58.     Transaction tr = Transaction.begin(ObjectStore.UPDATE);
  59.  
  60.     // Create instances of Person:
  61.  
  62.     Person sophie = new Person("Sophie", 5, null);
  63.     Person joseph = new Person("Joseph", 1, null);
  64.     Person children[] = { sophie, joseph };
  65.     Person tim = new Person("Tim", 35, children);
  66.  
  67.     // Create a database root and associate it with 
  68.     // tim, which is a persistent-capable object.
  69.     // ObjectStore Java uses a database root as an entry
  70.     // point into a database. 
  71.  
  72.     db.createRoot("Tim", tim);
  73.  
  74.     // End the transaction. This stores the three person objects,
  75.     // along with the String objects representing their names, and
  76.     // the array of children, into the database. 
  77.  
  78.     tr.commit();
  79.  
  80.     return db;
  81.   }
  82.  
  83.   static void readDatabase(Database db) {
  84.  
  85.     // Start a read-only transaction:
  86.  
  87.     Transaction tr = Transaction.begin(ObjectStore.READONLY);
  88.  
  89.     // Use the "Tim" database root to access objects in the database.
  90.     // Because tim references sophie and joseph, obtaining the "Tim"
  91.     // database root allows the program to also reach sophie and joseph.
  92.     // In each transaction, an application must obtain a database root 
  93.     // and use it to navigate to persistent objects. Be sure to write
  94.     // your application so that it does not hold on to references
  95.     // to persistent objects between transactions.
  96.  
  97.     Person tim = (Person)db.getRoot("Tim");
  98.     Person children[] = tim.getChildren();
  99.     System.out.print("Tim is " + tim.getAge() + " and has " +
  100.                children.length + " children named: ");
  101.     for (int i=0; i<children.length; i++) {
  102.       String name = children[i].getName();
  103.       System.out.print(name + " ");
  104.     }
  105.     System.out.println("");
  106.     // End the read-only transaction. This ends the
  107.     // accessibility of the persistent objects and abandons
  108.     // the transient objects.
  109.  
  110.     tr.commit();
  111.   }
  112.  
  113.   // Constructor:
  114.  
  115.   public Person(String name, int age, Person children[]) {
  116.     this.name = name; this.age = age; this.children = children;
  117.   }
  118.  
  119.   public String getName() { return name; }
  120.   public void setName(String name) { this.name = name; }
  121.   public int getAge() { return age; }
  122.   public void setAge(int age) { this.age = age; }
  123.   public Person[] getChildren() { return children; }
  124.   public void setChildren(Person children[]) {
  125.     this.children = children;
  126.   }
  127.    
  128.   // This class is never used as a persistent hash key.
  129.   public int hashCode() {
  130.     return super.hashCode();
  131.   }
  132.  
  133. }
  134.  
  135.