Difference between revisions of "EclipseLink/Examples/Foundation/DynamicPersistence"

From Eclipsepedia

Jump to: navigation, search
(Example)
(Example)
 
(2 intermediate revisions by one user not shown)
Line 14: Line 14:
  
 
<source lang="java5">
 
<source lang="java5">
//build a shared session (a.k.a. <tt>ServerSession</tt>) - deal with database userid/password/url details
+
//build a shared session (a.k.a. ServerSession) - deal with database userid/password/url details
 
...
 
...
DynamicHelper helper = new DynamicHelper(getSharedSession());
+
DynamicHelper helper = new DynamicHelper(sharedSession);
 
DynamicClassLoader dcl = helper.getDynamicClassLoader();
 
DynamicClassLoader dcl = helper.getDynamicClassLoader();
 
Class<?> employeeClass= dcl.createDynamicClass("scott.Emp");
 
Class<?> employeeClass= dcl.createDynamicClass("scott.Emp");
 
Class<?> deptClass= dcl.createDynamicClass("scott.Dept");
 
Class<?> deptClass= dcl.createDynamicClass("scott.Dept");
DynamicTypeBuilder employeeBuilder = new DynamicTypeBuilder(employeeClass, null /*no parent type*/,  
+
DynamicTypeBuilder employeeBuilder = new DynamicTypeBuilder(employeeClass,
  "EMP");
+
    null /*no parent type*/, "EMP");
 +
DynamicTypeBuilder deptBuilder = new DynamicTypeBuilder(deptClass,
 +
    null /*no parent type*/, "DEPT");
 
employeeBuilder.setPrimaryKeyFields("EMPNO");
 
employeeBuilder.setPrimaryKeyFields("EMPNO");
 
employeeBuilder.addDirectMapping("employeeNumber", int.class, "EMPNO");
 
employeeBuilder.addDirectMapping("employeeNumber", int.class, "EMPNO");
Line 30: Line 32:
 
employeeBuilder.addDirectMapping("commision", int.class, "COMM");
 
employeeBuilder.addDirectMapping("commision", int.class, "COMM");
 
employeeBuilder.addOneToOneMapping("manager", employeeBuilder.getType(), "MGR");
 
employeeBuilder.addOneToOneMapping("manager", employeeBuilder.getType(), "MGR");
DynamicTypeBuilder deptBuilder = new DynamicTypeBuilder(deptClass, null /*no parent type*/,
 
  "DEPT");
 
 
deptBuilder.setPrimaryKeyFields("DEPTNO");
 
deptBuilder.setPrimaryKeyFields("DEPTNO");
 
deptBuilder.addDirectMapping("deptNumber", int.class, "DEPTNO");
 
deptBuilder.addDirectMapping("deptNumber", int.class, "DEPTNO");
 
deptBuilder.addDirectMapping("name", String.class, "DNAME");
 
deptBuilder.addDirectMapping("name", String.class, "DNAME");
 
deptBuilder.addDirectMapping("location", String.class, "LOC");
 
deptBuilder.addDirectMapping("location", String.class, "LOC");
employeeBuilder.addOneToManyMapping("departments", deptBuilder.getType(), "DEPTNO ");
+
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");
 +
//query.addAscendingOrdering("employeeNumber");
 +
query.addAscendingOrdering("name");
 +
List<DynamicEntity> employees = (List<DynamicEntity>)sharedSession.executeQuery(query);
 
</source>
 
</source>

Latest revision as of 16:49, 16 December 2009


[edit] Simple Example using EclipseLink Dynamic Persistence

[edit] Definition

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.

[edit] Example

ScottTigerEmpDept.png

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.setPrimaryKeyFields("EMPNO");
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.setPrimaryKeyFields("DEPTNO");
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");
//query.addAscendingOrdering("employeeNumber");
query.addAscendingOrdering("name");
List<DynamicEntity> employees = (List<DynamicEntity>)sharedSession.executeQuery(query);