Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
class Person (1) - file: Person.java 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(..): see slide 3 • createDatabase(..): see slide 4&5 • readDatabase(..): see slide 6 // Constructor: public Person(String name, int age, Person children[]) { this.name = name; this.age = age; this.children = children; } 1 class Person (2) - file: Person.java - public public public public public public String getName() {return name;} void setName(String name) {this.name = name;} int getAge() {return age;} void setAge(int age) {this.age = age;} Person[] getChildren() {return children;} 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(); } }//Person 2 1 main(..) class Person - file: Person.java - // 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(); } } 3 createDatabase(..) (1) class Person - file: Person.java - 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.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); 4 2 createDatabase(..) (2) class Person - file: Person.java - // 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; } 5 readDatabase(..) class Person - file: Person.java - 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(); } 6 3