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



-- 
View this message in context: 
http://www.nabble.com/JPA%3A-adding-entities-to-EntityManagerFactory-programmatically-tp17500835p17526048.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Reply via email to