Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Difference between revisions of "EclipseLink/Development/Dynamic/Design RuntimeDefinition"
< EclipseLink | Development | Dynamic
Line 11: | Line 11: | ||
Here is a simple example using the native ORM API: | Here is a simple example using the native ORM API: | ||
− | <source lang=" | + | <source lang="java5"> |
public void entityTypeFromDescriptor() throws Exception { | public void entityTypeFromDescriptor() throws Exception { | ||
EntityTypeImpl entityType = buildMyEntityType(); | EntityTypeImpl entityType = buildMyEntityType(); |
Revision as of 14:46, 22 September 2009
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