Skip to main content
Jump to: navigation, search

Difference between revisions of "EclipseLink/Examples/JPA/Dynamic"

(Define a new Dynamic Entity Type)
Line 1: Line 1:
<div style="border:1px solid #999999;background-color:#ffffff;align:center">
<div style="border:1px solid #999999;background-color:#ffffff;align:center">
<table border="0" cellpadding="5" align="center"><tr><td width="20">[[image:Catnicon.gif]]</td><td>This page is under construction. See bug [ 225026] for more information</td></tr></table>
<table border="0" cellpadding="5" align="center"><tr><td width="20">[[image:Catnicon.gif]]</td><td>This page is under construction. See bug [ 225026] for more informationand to provide feedback on this work-in-progress.</td></tr></table>

Revision as of 13:55, 2 April 2008

Catnicon.gifThis page is under construction. See bug 225026 for more informationand to provide feedback on this work-in-progress.

How to use Dynamic JPA persistence

This how-to illustrates how EclipseLink can be extended to deliver all of its JPA functionality in a dynamic scenario where no Java class exists for the entity and optionally where no XML is provided. This allows the EclipseLink JPA solution to be used in more dynamic environments where either the mode is unknown at development time or the application is simply dynamic in nature.

Dynamic Entity Extension

The extension to EclipseLink to enable dynamic entities and the associated helpers involves the following classes.



The dynamic entity extension is currently illustrated through test cases in the example.

Creating and Entity on the Fly

In this example (
) an EntityManagerFactory is bootstrapped without a persistence.xml and then a dynamic entity type is defined. After this the dynamic entity can be used through standard JPA queries and transactions.

Creating the EMF

DynamicClassLoader loader = new DynamicClassLoader(null,
SEPersistenceUnitInfo puInfo = new SEPersistenceUnitInfo();
// This root URL config should go away when bug 225321  is fixed
		.setPersistenceUnitRootUrl(new URL(
EntityManagerSetupImpl setup = new EntityManagerSetupImpl();
setup.predeploy(puInfo, null);
// This replaces Persistence.createEntityManagerFactory
EntityManagerFactory emf = new EntityManagerFactoryImpl(setup,

Define a new Dynamic Entity Type

The EntityTypeFactory class provides some useful methods to simplify creating a dynamic type avoiding some of the complexities.

EntityType employeeType = EntityTypeFactory.create("Employee",
		"DynamicEntity$Employee", "DYNAMIC_EMP", loader);
EntityTypeFactory.addBasicProperty(employeeType, "id", "EMP_ID",
		Integer.class, true);
EntityTypeFactory.addBasicProperty(employeeType, "firstName", "F_NAME",
		String.class, false);
EntityTypeFactory.addBasicProperty(employeeType, "lastName", "L_NAME",
		String.class, false);
EntityTypeFactory.addBasicProperty(employeeType, "salary", "SALARY",
		double.class, false);
EntityTypeFactory.addToPersistenceUnit(emf, employeeType);

Creating the Tables

EclipseLink's SchemaManager can be used to drop/create the tables. Use with caution.

new SchemaManager(JpaHelper.getServerSession(emf))

The SQL generated when run against and Oracle database appears as:

[EPS Fine]: Connection(1741620)--DROP TABLE DYNAMIC_EMP

The schema definition types can be customized if required.

Using the Dynamic Entity

After the dynamic entity type is properly defined and initialized you can use it. The only real tricks to using a dynamic type versus a static type is that:

  • you must use the DynamicEntity API to access and manipulate your data.
  • any time you need the Java class you must use the generated class. This generated class is held within the EclipseLink mapping metadata but is more easily accessible using the EntityType. The DynamicJpaHelper can also be used to lookup the type.
  • creating a new instance requires the usage of a factory method on EntityType
EntityManager em = emf.createEntityManager();
DynamicEntity emp1 = (DynamicEntity) em.find(employeeType
		.getJavaClass(), 1);
emp1 = employeeType.newInstance();
emp1.set("id", 1);
emp1.set("firstName", "John");
emp1.set("lastName", "Doe");
emp1 = (DynamicEntity) em.find(employeeType.getJavaClass(), 1);
emp1 = (DynamicEntity) em
				"SELECT e FROM Employee e WHERE e.firstName = :FNAME AND e.lastName = :LNAME")
		.setParameter("FNAME", "John").setParameter("LNAME", "Doe")

Dynamic Entities using ORM.XML

This does not yet work - see TO DO list

Try It Out

The example is being developed with the EclipseLink Example component at:


In the future a zip will be available download with setup and run instructions.

Outstanding Work

This how-to is based on an extension to EclipseLink JPA. The functionality needs to be consolidated into the project leveraging the existing dynamic support introduced in EclipseLink SDO.


  • Add support for using JPA ORM.XML and EclipseLink ORM.XML. At present the metadata processor always validates the existence of the field or standard get method on the class.
  • Consolidate the SDO approach for a base dynamic entity class with this one in the Foundation component
  • Minimize the introduction of yet another meta model. This will be done either through usage of the existing Descriptor/Mapping model or through some generalization of the SDO meta-model basics within Foundation.
  • Simplify DynamicEntity usage by subclassing AbstractMap<String, Object>

Back to the top