Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

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

(Creating and Entity on the Fly)
Line 8: Line 8:
  
 
== Dynamic Entity Extension ==
 
== Dynamic Entity Extension ==
 +
 +
The extension to EclipseLink to enable dynamic entities and the associated helpers involves the following classes.
 +
 +
TBD
  
 
== Example ==
 
== Example ==
Line 21: Line 25:
  
 
<source lang=java>
 
<source lang=java>
DynamicClassLoader loader = new DynamicClassLoader(null,
+
DynamicClassLoader loader = new DynamicClassLoader(null,
DynamicEntity.class);
+
DynamicEntity.class);
SEPersistenceUnitInfo puInfo = new SEPersistenceUnitInfo();
+
SEPersistenceUnitInfo puInfo = new SEPersistenceUnitInfo();
puInfo.setClassLoader(loader);
+
puInfo.setClassLoader(loader);
puInfo.setPersistenceUnitName("dynamic");
+
puInfo.setPersistenceUnitName("dynamic");
// This root URL config should go away when bug 225321  is fixed
+
// This root URL config should go away when bug 225321  is fixed
puInfo
+
puInfo
.setPersistenceUnitRootUrl(new URL(
+
.setPersistenceUnitRootUrl(new URL(
"file:/C:/Eclipse/EclipseLink/trunk/examples/org.eclipse.persistence.example.jpa.dynamic/classes/"));
+
"file:/C:/Eclipse/EclipseLink/trunk/examples/org.eclipse.persistence.example.jpa.dynamic/classes/"));
 
+
EntityManagerSetupImpl setup = new EntityManagerSetupImpl();
+
setup.predeploy(puInfo, null);
+
// This replaces Persistence.createEntityManagerFactory
+
EntityManagerFactory emf = new EntityManagerFactoryImpl(setup,
+
EclipseLinkJPATest.getEMFProperties());
+
  
 +
EntityManagerSetupImpl setup = new EntityManagerSetupImpl();
 +
setup.predeploy(puInfo, null);
 +
// This replaces Persistence.createEntityManagerFactory
 +
EntityManagerFactory emf = new EntityManagerFactoryImpl(setup,
 +
EclipseLinkJPATest.getEMFProperties());
 
</source>
 
</source>
  
Line 42: Line 45:
  
 
<source lang=java>
 
<source lang=java>
EntityType employeeType = EntityTypeFactory.create("Blah",
+
EntityType employeeType = EntityTypeFactory.create("Blah",
"model.Blah", "BLAH", loader);
+
"model.Blah", "BLAH", loader);
EntityTypeFactory.addBasicProperty(employeeType, "id", "ID",
+
EntityTypeFactory.addBasicProperty(employeeType, "id", "ID",
Integer.class, true);
+
Integer.class, true);
EntityTypeFactory.addBasicProperty(employeeType, "firstName", "F_NAME",
+
EntityTypeFactory.addBasicProperty(employeeType, "firstName", "F_NAME",
String.class, false);
+
String.class, false);
EntityTypeFactory.addBasicProperty(employeeType, "lastName", "L_NAME",
+
EntityTypeFactory.addBasicProperty(employeeType, "lastName", "L_NAME",
String.class, false);
+
String.class, false);
EntityTypeFactory.addToPersistenceUnit(emf, employeeType);
+
EntityTypeFactory.addToPersistenceUnit(emf, employeeType);
 
+
Assert.assertEquals(Integer.class, employeeType.getDescriptor()
+
.getCMPPolicy().getPKClass());
+
 
</source>
 
</source>
  
Line 61: Line 61:
  
 
<source lang=java>
 
<source lang=java>
new SchemaManager(JpaHelper.getServerSession(emf))
+
new SchemaManager(JpaHelper.getServerSession(emf))
.replaceDefaultTables();
+
.replaceDefaultTables();
 
</source>
 
</source>
  
Line 68: Line 68:
  
 
<source lang=java>
 
<source lang=java>
EntityManager em = emf.createEntityManager();
+
EntityManager em = emf.createEntityManager();
  
DynamicEntity blah1 = (DynamicEntity) em.find(employeeType
+
DynamicEntity blah1 = (DynamicEntity) em.find(employeeType
.getJavaClass(), 1);
+
.getJavaClass(), 1);
Assert.assertNull(blah1);
+
Assert.assertNull(blah1);
  
em.getTransaction().begin();
+
em.getTransaction().begin();
  
blah1 = employeeType.newInstance();
+
blah1 = employeeType.newInstance();
blah1.set("id", 1);
+
blah1.set("id", 1);
  
em.persist(blah1);
+
em.persist(blah1);
em.getTransaction().commit();
+
em.getTransaction().commit();
  
em.clear();
+
em.clear();
  
blah1 = (DynamicEntity) em.find(employeeType.getJavaClass(), 1);
+
blah1 = (DynamicEntity) em.find(employeeType.getJavaClass(), 1);
Assert.assertNotNull(blah1);
+
Assert.assertNotNull(blah1);
 
</source>
 
</source>
  

Revision as of 13:08, 2 April 2008

Catnicon.gifThis page is under construction. See bug 225026 for more information

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.

TBD

Example

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


Creating and Entity on the Fly

In this example (
testing.PUWithoutXML#createSimpleDynamicEntity()
) 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,
		DynamicEntity.class);
SEPersistenceUnitInfo puInfo = new SEPersistenceUnitInfo();
puInfo.setClassLoader(loader);
puInfo.setPersistenceUnitName("dynamic");
// This root URL config should go away when bug 225321  is fixed
puInfo
		.setPersistenceUnitRootUrl(new URL(
				"file:/C:/Eclipse/EclipseLink/trunk/examples/org.eclipse.persistence.example.jpa.dynamic/classes/"));
 
EntityManagerSetupImpl setup = new EntityManagerSetupImpl();
setup.predeploy(puInfo, null);
// This replaces Persistence.createEntityManagerFactory
EntityManagerFactory emf = new EntityManagerFactoryImpl(setup,
		EclipseLinkJPATest.getEMFProperties());

Define a new Dynamic Entity Type

EntityType employeeType = EntityTypeFactory.create("Blah",
		"model.Blah", "BLAH", loader);
EntityTypeFactory.addBasicProperty(employeeType, "id", "ID",
		Integer.class, true);
EntityTypeFactory.addBasicProperty(employeeType, "firstName", "F_NAME",
		String.class, false);
EntityTypeFactory.addBasicProperty(employeeType, "lastName", "L_NAME",
		String.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))
		.replaceDefaultTables();

Using the Dynamic Entity

EntityManager em = emf.createEntityManager();
 
DynamicEntity blah1 = (DynamicEntity) em.find(employeeType
		.getJavaClass(), 1);
Assert.assertNull(blah1);
 
em.getTransaction().begin();
 
blah1 = employeeType.newInstance();
blah1.set("id", 1);
 
em.persist(blah1);
em.getTransaction().commit();
 
em.clear();
 
blah1 = (DynamicEntity) em.find(employeeType.getJavaClass(), 1);
Assert.assertNotNull(blah1);

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:

/svnroot/technology/org.eclipse.persistence/trunk/examples/org.eclipse.persistence.example.jpa.dynamic

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.

TO DO:

  • 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.

Back to the top