You are here: Advanced Features > db4o Meta-Information

db4o Meta-Information

Db4o meta information API provides an access to the actual structure of db4o database file. Its primary use is refactoring.

You can access the meta information via extended object container. You can ask the object container for all stored classes or for a specific class. To find the meta information for a specific class you can provide the full name, the class itself or an instance of a particular type.

Note that db4o also returns information about internal db4o instances which have been stored.

// Get the information about all stored classes.
StoredClass[] classesInDB = container.ext().storedClasses();
for (StoredClass storedClass : classesInDB) {
    System.out.println(storedClass.getName());
}

// Information for a certain class
StoredClass metaInfo = container.ext().storedClass(Person.class);
MetaInfoExample.java: All stored classes

The stored class interface provides all meta information db4o knows about. You can get the name of the class, ask for the instance count, ask for a list of the ids and get the meta info for super classes.

The most important information about the stored classes meta info is the list of the field which are stored. You can get a list of all fields or ask for specific fields. Note that the meta information might return information for fields which don't exist anymore. This is useful for refactoring.

StoredClass metaInfoForPerson = container.ext().storedClass(Person.class);
// Access all existing fields
for (StoredField field : metaInfoForPerson.getStoredFields()) {
    System.out.println("Field: "+field.getName());
}
// Accessing the field 'name' of any type.
StoredField nameField = metaInfoForPerson.storedField("name", null);
// Accessing the string field 'name'. Important if this field had another time in previous
// versions of the class model
StoredField ageField = metaInfoForPerson.storedField("age",int.class);

// Check if the field is indexed
boolean isAgeFieldIndexed = ageField.hasIndex();

// Get the type of the field
String fieldType = ageField.getStoredType().getName();
MetaInfoExample.java: Accessing stored fields

On a the field meta information you can find out the name, type and if the field has an index. And you also can access the values of a object via the stored field. This allows you to access information which is stored in the database but has been removed from the class model. This is useful for refactoring.

StoredClass metaForPerson = container.ext().storedClass(Person.class);
StoredField metaNameField = metaForPerson.storedField("name", null);

ObjectSet<Person> persons = container.query(Person.class);
for (Person person : persons) {
    String name = (String)metaNameField.get(person);
    System.out.println("Name is "+name);
}
MetaInfoExample.java: Access via meta data