Create Entity Manager Factory without persistence.

2019-04-14 13:39发布

Is there a way to create the EntityManagerFactory without a persistence unit defined? Can you give all the required properties to create an entity manager factory? I need to create the EntityManagerFactory from the user's specified values at runtime. Updating the persistence.xml and recompiling is not an option.

Currently I am using eclipselink dynamic persistence so i need to create the EntityManagerFactory without a persistence unit defined ? I have groups of runtime entities that need to map single group of entities to different database in runtime and no persistence unit entry is available for this runtime group of entities .

Any idea on how to do this is more than welcomed!

2条回答
手持菜刀,她持情操
2楼-- · 2019-04-14 14:03

Just seen in the DataNucleus documentation:

import org.datanucleus.metadata.PersistenceUnitMetaData;
import org.datanucleus.api.jpa.JPAEntityManagerFactory;

PersistenceUnitMetaData pumd = new PersistenceUnitMetaData("dynamic-unit", "RESOURCE_LOCAL", null);
pumd.addClassName("org.datanucleus.test.A");
pumd.setExcludeUnlistedClasses();
pumd.addProperty("javax.persistence.jdbc.url", "jdbc:h2:mem:nucleus");
pumd.addProperty("javax.persistence.jdbc.driver", "org.h2.Driver");
pumd.addProperty("javax.persistence.jdbc.user", "sa");
pumd.addProperty("javax.persistence.jdbc.password", "");
pumd.addProperty("datanucleus.autoCreateSchema", "true");

EntityManagerFactory emf = new JPAEntityManagerFactory(pumd, null);

http://www.datanucleus.org/products/accessplatform_3_2/jpa/persistence_unit.html

查看更多
别忘想泡老子
3楼-- · 2019-04-14 14:14

Your best option is most likely to access the PersistenceProvider directly and use the EJB container API to create the EntityManagerFactory from a PersistenceUnitInfo.

PersistenceProvider.createContainerEntityManagerFactory()

See, http://www.eclipse.org/eclipselink/api/2.5/org/eclipse/persistence/jpa/PersistenceProvider.html#createContainerEntityManagerFactory%28javax.persistence.spi.PersistenceUnitInfo,%20java.util.Map%29

查看更多
登录 后发表回答