Skip to main content
 首页 » 编程设计

jpa之创建没有 persistence.xml 配置文件的 JPA EntityManager

2024年02月27日44kenshinobiy

有没有办法在没有定义持久性单元的情况下初始化EntityManager?您能否提供创建实体管理器所需的所有属性?我需要在运行时根据用户指定的值创建 EntityManager 。更新 persistence.xml 并重新编译不是一个选项。

任何关于如何做到这一点的想法都非常受欢迎!

请您参考如下方法:

Is there a way to initialize the EntityManager without a persistence unit defined?

您应该在 persistence.xml 部署描述符中定义至少一个持久性单元。

Can you give all the required properties to create an Entitymanager?

  • 名称属性是必需的。其他属性和元素是可选的。 (JPA 规范)。因此,这或多或少应该是您的最小 persistence.xml 文件:
<persistence> 
    <persistence-unit name="[REQUIRED_PERSISTENCE_UNIT_NAME_GOES_HERE]"> 
        SOME_PROPERTIES 
    </persistence-unit> 
</persistence> 

In Java EE environments, the jta-data-source and non-jta-data-source elements are used to specify the global JNDI name of the JTA and/or non-JTA data source to be used by the persistence provider.

因此,如果您的目标应用程序服务器支持 JTA(JBoss、Websphere、GlassFish),您的 persistence.xml 如下所示:

<persistence> 
    <persistence-unit name="[REQUIRED_PERSISTENCE_UNIT_NAME_GOES_HERE]"> 
        <!--GLOBAL_JNDI_GOES_HERE--> 
        <jta-data-source>jdbc/myDS</jta-data-source> 
    </persistence-unit> 
</persistence> 

如果您的目标应用程序服务器不支持 JTA (Tomcat),您的 persistence.xml 如下所示:

<persistence> 
    <persistence-unit name="[REQUIRED_PERSISTENCE_UNIT_NAME_GOES_HERE]"> 
        <!--GLOBAL_JNDI_GOES_HERE--> 
        <non-jta-data-source>jdbc/myDS</non-jta-data-source> 
    </persistence-unit> 
</persistence> 

如果您的数据源未绑定(bind)到全局 JNDI(例如,在 Java EE 容器外部),那么您通常会定义 JPA 提供程序、驱动程序、url、用户和密码属性。 但是属性名称取决于 JPA 提供商。因此,对于 Hibernate 作为 JPA 提供者,您的 persistence.xml 文件将如下所示:

<persistence> 
    <persistence-unit name="[REQUIRED_PERSISTENCE_UNIT_NAME_GOES_HERE]"> 
        <provider>org.hibernate.ejb.HibernatePersistence</provider> 
        <class>br.com.persistence.SomeClass</class> 
        <properties> 
            <property name="hibernate.connection.driver_class" value="org.apache.derby.jdbc.ClientDriver"/> 
            <property name="hibernate.connection.url" value="jdbc:derby://localhost:1527/EmpServDB;create=true"/> 
            <property name="hibernate.connection.username" value="APP"/> 
            <property name="hibernate.connection.password" value="APP"/> 
        </properties> 
    </persistence-unit> 
</persistence> 

交易类型属性

In general, in Java EE environments, a transaction-type of RESOURCE_LOCAL assumes that a non-JTA datasource will be provided. In a Java EE environment, if this element is not specified, the default is JTA. In a Java SE environment, if this element is not specified, a default of RESOURCE_LOCAL may be assumed.

  • 为了确保 Java SE 应用程序的可移植性,有必要显式列出持久化单元中包含的托管持久化类(JPA 规范)

I need to create the EntityManager from the user's specified values at runtime

所以使用这个:

Map addedOrOverridenProperties = new HashMap(); 
 
// Let's suppose we are using Hibernate as JPA provider 
addedOrOverridenProperties.put("hibernate.show_sql", true); 
 
Persistence.createEntityManagerFactory(<PERSISTENCE_UNIT_NAME_GOES_HERE>, addedOrOverridenProperties);