Skip to main content

Importing a Schema

Importing a Schema

Before an instance of a Java class can be stored as a persistent event, a schema must be imported for the class. The schema defines the database structure in which the event will be stored. XEP provides two different schema import models: flat schema and full schema. The main difference between these models is the way in which Java objects are projected to Caché events. A flat schema is the optimal choice if performance is essential and the event schema is fairly simple. A full schema offers a richer set of features for more complex schemas, but may have an impact on performance. See “Schema Customization and Mapping” for a detailed discussion of schema models and related subjects.

The following methods are used to analyze a Java class and import a schema of the desired type:

  • EventPersister.importSchema() — imports a flat schema. Takes an argument specifying a .jar file name, a fully qualified class name, or an array of class names, and imports all classes and any dependencies found in the specified locations. Returns a String array containing the names of all successfully imported classes.

  • EventPersister.importSchemaFull() — imports a full schema. Takes the same arguments and returns the same class list as importSchema(). A class imported by this method must declare a user-generated IdKey (see “Using IdKeys”).

  • Event.isEvent() — is a static Event method that takes a Java object or class name of any type as an argument, tests to see if the specified object can be projected as a valid XEP event (see “Requirements for Imported Classes”), and throws an appropriate error if it is not valid.

The import methods are identical except for the schema model used. The following example imports a simple test class and its dependent class:

Importing a Schema: Importing a class and its dependencies

The following classes from package test are to be imported:

  public class MainClass {
    public MainClass() {}
    public String  myString;
    public test.Address  myAddress;
  }

  public class Address {
    public String  street;
    public String  city;
    public String  state;
  }

The following code uses importSchema() to import the main class, test.MainClass, after calling isEvent() to make sure it can be projected. Dependent class test.Address is also imported automatically when test.MainClass is imported:

  try {
    Event.isEvent("test.MainClass"); // throw an exception if class is not projectable
    myPersister.importSchema("test.MainClass");
  }
  catch (XEPException e) {System.out.println("Import failed:\n" + e);}

In this example, instances of dependent class test.Address will be serialized and embedded in the same Caché object as other fields of test.MainClass. If importSchemaFull() had been used instead, stored instances of test.MainClass would contain references to instances of test.Address stored in a separate Caché class extent.

FeedbackOpens in a new tab