0

我使用 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);

但我不仅需要阅读属性。

4

0 回答 0