EclipseLink/Development/Dynamic/Design RuntimeDefinition

From Eclipsepedia

Jump to: navigation, search

Contents

Dynamic Persistence Design: Create/Modify/Remove Dynamic Types

Dynamic persistence also allows for the creation of the dynamic types during the runtime execution of an application. This is generally in response to an administrator user defining a new type with its properties and associations and the requesting the type to be added and optionally its schema to be created on the database. This is accomplished in this design using the EntityTypeBuilder which functions as a factory for building or modifying a dynamic type.

EntityTypeBuilder API

The API on the EntityTypeBuilder allows a type to be wrapped and extended. The wrapped type can then be added to a session.

Creating new Types

Here is a simple example using the native ORM API:

public void entityTypeFromDescriptor() throws Exception {
    EntityTypeImpl entityType = buildMyEntityType();
 
    assertEquals(MyEntity.class, entityType.getJavaClass());
 
    DatabaseSession session = new Project(buildDatabaseLogin()).createDatabaseSession();
    session.getSessionLog().setLevel(SessionLog.FINE);
    session.login();
 
    session.addDescriptor(entityType.getDescriptor());
    new SchemaManager(session).replaceDefaultTables();
 
    DynamicEntity entity = entityType.newInstance();
    entity.set("id", 1);
    entity.set("name", "Name");
 
    session.insertObject(entity);
 
    session.logout();
 
}
 
private EntityTypeImpl buildMyEntityType() {
    EntityTypeBuilder factory = new EntityTypeBuilder(MyEntity.class, null, "MY_ENTITY");
    factory.setPrimaryKeyFields("ID");
    factory.addDirectMapping("id", int.class, "ID");
    factory.addDirectMapping("name", String.class, "NAME");
 
    return (EntityTypeImpl) factory.getType();
}

A more comprehensive example is available here

Modifying Existing Types

TBD

Removing Existing Types

Is this just 'de-activating' the descriptor? What happens to 'live' objects?