Skip to main content

Notice: This Wiki is now read only and edits are no longer possible. Please see: for the plan.

Jump to: navigation, search


Simple Example using EclipseLink Dynamic Persistence


Dynamic Persistence is defined as the ability to create a dynamic persistent entity and use it within an application without a-priori the entity's Java .class being present on the classpath (or in some .jar/.war archive).

The purpose of Dynamic Persistence is to enable simplified data access where only mapping information is required and no concrete Java model is required. This allows applications with dynamic storage requirements to avoid coupling to static classes or require specialized handling of new types. The application uses standard EclipseLink APIs to read, create, modify, and remove dynamic persistent entities from their data stores based on types that are either defined in XML mapping files or are constructed within the running application.



The classes from the new package org.eclipse.persistence.dynamic are used to construct a simple persistence example: DynamicClassLoader, DynamicHelper, and DynamicTypeBuilder:

//build a shared session (a.k.a. ServerSession) - deal with database userid/password/url details
DynamicHelper helper = new DynamicHelper(sharedSession);
DynamicClassLoader dcl = helper.getDynamicClassLoader();
Class<?> employeeClass= dcl.createDynamicClass("scott.Emp");
Class<?> deptClass= dcl.createDynamicClass("scott.Dept");
DynamicTypeBuilder employeeBuilder = new DynamicTypeBuilder(employeeClass,
    null /*no parent type*/, "EMP");
DynamicTypeBuilder deptBuilder = new DynamicTypeBuilder(deptClass,
    null /*no parent type*/, "DEPT");
employeeBuilder.addDirectMapping("employeeNumber", int.class, "EMPNO");
employeeBuilder.addDirectMapping("name", String.class, "ENAME");
employeeBuilder.addDirectMapping("job", String.class, "JOB");
employeeBuilder.addDirectMapping("hiredOn", Date.class, "HIREDATE");
employeeBuilder.addDirectMapping("salaryPerWeek", int.class, "SAL");
employeeBuilder.addDirectMapping("commision", int.class, "COMM");
employeeBuilder.addOneToOneMapping("manager", employeeBuilder.getType(), "MGR");
deptBuilder.addDirectMapping("deptNumber", int.class, "DEPTNO");
deptBuilder.addDirectMapping("name", String.class, "DNAME");
deptBuilder.addDirectMapping("location", String.class, "LOC");
employeeBuilder.addOneToOneMapping("department", deptBuilder.getType(), "DEPTNO");
helper.addTypes(false /*dont create tables*/, false /*dont create constraints*/,
    employeeBuilder.getType(), deptBuilder.getType());
ReadAllQuery query = helper.newReadAllQuery("Emp");
List<DynamicEntity> employees = (List<DynamicEntity>)sharedSession.executeQuery(query);

Back to the top