Jump to: navigation, search

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

(Example)
 
(5 intermediate revisions by the same user not shown)
Line 10: Line 10:
 
=== Example ===
 
=== Example ===
 
[[Image:ScottTigerEmpDept.png|650px]]
 
[[Image:ScottTigerEmpDept.png|650px]]
 +
 
The classes from the new package <tt>org.eclipse.persistence.dynamic</tt> are used to construct a simple persistence example: <tt>DynamicClassLoader</tt>, <tt>DynamicHelper</tt>, and <tt>DynamicTypeBuilder:
 
The classes from the new package <tt>org.eclipse.persistence.dynamic</tt> are used to construct a simple persistence example: <tt>DynamicClassLoader</tt>, <tt>DynamicHelper</tt>, and <tt>DynamicTypeBuilder:
  
 
<source lang="java5">
 
<source lang="java5">
//build a shared session (a.k.a. <tt>ServerSession</tt>)
+
//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<?> javaType = dcl.createDynamicClass("model.Simple");
+
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);
 
</source>
 
</source>

Latest revision as of 16:49, 16 December 2009


Simple Example using EclipseLink Dynamic Persistence

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.

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);