openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From RĂ¼diger Herrmann <rherrm...@innoopract.com>
Subject Re: JPA: adding entities to EntityManagerFactory programmatically
Date Thu, 29 May 2008 07:11:24 GMT
HURRAY, the example is working!

But making the same changes to my PDE TestCases fails. I am pretty 
sure this is because of different class loaders.

Is it possible to also specify a distinct class loader for each 
entity class?

Background:
I am running in an OSGi environment in which the code is modularized 
into bundles. Each bundle has its own class loader.
The EntityManager lives in bundle A that has class loader CA. The 
entity class lives in bundle B that has class loader CB. Further 
entities could live in bundle C with class loader CC. And so on...
For now, the class loaders are already known when the the 
EntityManagerFactory properties need to be constructed.

Is there a way to do something like this?
   Map<String, String> properties = new HashMap<String, String>();
   properties.put( "openjpa.MetaDataFactory",
                   "jpa(Types=org.ex.Person:org.osgi.BundleLoader)");

Thanks in advance
RĂ¼diger

Pinaki Poddar wrote:
> Hi,
>    If the list of persistent classes are known *before* EntityManagerFactory
> is created, then a possible
> solution is as below:
> 
> public class TestDynamic extends TestCase {
> 	public void testDynamic() {
>                // create a semicolon-separated list of persistent class
> names
> 		Class[] classes = { Unenhanced.class};
> 		StringBuffer classNames = new StringBuffer();
> 		for (Class c : classes) {
> 			if (classNames.length() > 0)
> 				classNames.append(";");
> 			classNames.append(c.getName());
> 		}
> 
> 		Map<String, String> properties = new HashMap<String, String>();
> 		properties.put("openjpa.RuntimeUnenhancedClasses", "supported");
> 		properties.put("openjpa.ConnectionUserName", "sa");
> 		properties.put("openjpa.ConnectionPassword", "");
> 		properties.put("openjpa.ConnectionURL", "jdbc:mysql://localhost/test");
> 		properties.put("openjpa.ConnectionDriverName", "com.mysql.jdbc.Driver");
> 
>                // specify list of class names as a property
> 		properties.put("openjpa.MetaDataFactory", "jpa(Types="+classNames+")");
>  
>                 // allow table definitions on-the-fly
> 		properties.put("openjpa.jdbc.SynchronizeMappings", 
> "buildSchema(ForeignKeys=true)"); 
> 
>                // now create factory
> 		EntityManagerFactory factory =
> Persistence.createEntityManagerFactory("test", properties);
> 		
> 		// create a EM to trigger dynamic class management
> 		EntityManager em = factory.createEntityManager();
> 		em.close();
> 		
>                 // hence things should behave normally 
> 		em = factory.createEntityManager();
> 		Unenhanced pc = new Unenhanced();
> 		em.getTransaction().begin();
> 		em.persist(pc);
> 		em.getTransaction().commit();
> 	}
> 
> 
> 

Mime
View raw message