Download PSE Pro for Java User Guide

Transcript
Chapter 9: Generating Persistence-Capable Classes Manually
// Define a class that implements IPersistent:
class Person implements IPersistent {
// Fields:
String name;
int age;
Person children[];
// Other fields ...
// Constructor:
public Person(String name, int age, Person children[]) {
this.name = name; this.age = age; this.children = children;
}
// Hollow object constructor:
public Person(ClassInfo info) { }
// Accessor methods that have been modified to call
// the fetch() and dirty() methods:
public String getName() {ObjectStore.fetch(this);
return name; }
public void setName(String name) {ObjectStore.dirty(this);
this.name = name; }
public int getAge() {ObjectStore.fetch(this); return age; }
public void setAge(int age) {ObjectStore.dirty(this);
this.age = age; }
public Person[] getChildren() {ObjectStore.fetch(this);
return children; }
public void setChildren(Person children[]) {
ObjectStore.dirty(this); this.children = children;
}
// Other methods ...
// Additions required for PSE Pro:
// Define the initializeContents() method to load real
// values into hollow persistent objects, which makes
// them active persistent objects:
public void initializeContents(GenericObject handle) {
name = handle.getStringField(1, myClassInfo);
age = handle.getIntField(2, myClassInfo);
children = (Person[])handle.getArrayField(3, myClassInfo);
}
// Define the flushContents() method to copy the
// contents of a persistent object to the database:
public void flushContents(GenericObject handle) {
handle.setClassField(1, name, myClassInfo);
handle.setIntField(2, age, myClassInfo);
handle.setArrayField(3, children, myClassInfo);
}
Release 7.1
213