我使用JPA2并且使用在persistence.xml中表示的多持久性单元配置,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0">
<persistence-unit name="FirstPU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>entities.Book</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
...
</properties>
</persistence-unit>
<persistence-unit name="SecondPU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>entities.Book</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
...
</properties>
</persistence-unit>
</persistence>
是否有任何方法可以通过编程方式从persistence.xml中提取持久性单元名称(使用Hibernate或JPA2)?像这样:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("manager1");
// or
Map<String, Object> configOverrides = new HashMap<String, Object>();
configOverrides.put("hibernate.hbm2ddl.auto", "create-drop");
EntityManagerFactory programmaticEmf =
Persistence.createEntityManagerFactory("manager1", configOverrides);
但我需要阅读不仅是属性。