1 Reply Latest reply on Apr 17, 2014 12:09 AM by Stephen Coy

    Is there a way to create EntityManagerFactory programmatically without specifying pu name in persistence.xml?

    Richard Yang Apprentice

      Our application needs to be able to handle as many as 100 datasources. Most datasources are unknown at development time. So we will add the ad-hoc new datasources at runtime.  We also use JPA and the persistence.xml is embedded in the jar's meta-info folder. (Don't know why the JPA spec has to demand this, it makes dynamical adding new emf a nightmare). So we figure we can use Persistence.createEntityManagerFactory(pu name, overridding properties) to programmatically create one if we need. However, the problem that it seems that the PU name has to be specified in the persistence.xml. Without it, we got:Caused by: javax.persistence.PersistenceException: No Persistence provider for EntityManager named xxxxx

      Well, if we need to dynamically create an emf, what is the point to specify the pu name in persistence.xml?

       

      How do we get around this issue? I could have 100 PU placeholder names to be defined in the persistence.xml, but then create them when we need. But it is tedious to set up.

       

      I have essentially three questions.

      1) Assuming I am not using spring, is there anyway to have the persistence.xml defined outside the deployment archive and move it external location?

      2) Anyway not to specify the PU name in persistence.xml or specify one place holder and after the PU is created, change its name? I know it is not possible in early version of jboss, but is it possible in Wildfly?

      3) Andy other suggestions on how I can handle this dynamic datasources and PU situations?

       

       

      Thanks a lot in advance.