创建没有persistence.xml配置文件的JPA EntityManager [英] Create JPA EntityManager without persistence.xml configuration file

查看:34
本文介绍了创建没有persistence.xml配置文件的JPA EntityManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

非常欢迎有关如何执行此操作的任何想法!

解决方案

有没有办法在没有定义持久化单元的情况下初始化EntityManager?

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

<块引用>

您能否提供创建 Entitymanager 所需的所有属性?

  • name 属性是必需的.其他属性和元素是可选的.(JPA 规范).所以这或多或少应该是你最小的 persistence.xml 文件:

<persistence-unit name="[REQUIRED_PERSISTENCE_UNIT_NAME_GOES_HERE]">一些_属性</persistence-unit></持久性>

<块引用>

在 Java EE 环境中,jta-data-sourcenon-jta-data-source 元素用于指定全局 JNDI 名称JTA 和/或非 JTA 数据源供持久性提供者使用.

因此,如果您的目标应用服务器支持 JTA(JBoss、Websphere、GlassFish),您的 persistence.xml 看起来像:

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

如果您的目标应用服务器不支持 JTA (Tomcat),您的 persistence.xml 看起来像:

<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></持久性>

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

<persistence-unit name="[REQUIRED_PERSISTENCE_UNIT_NAME_GOES_HERE]"><provider>org.hibernate.ejb.HibernatePersistence</provider><class>br.com.persistence.SomeClass</class><属性><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"/></属性></persistence-unit></持久性>

交易类型属性

<块引用>

通常,在 Java EE 环境中,RESOURCE_LOCAL 的事务类型假定将提供非 JTA 数据源.在 Java EE 环境中,如果未指定此元素,则默认为 JTA.在 Java SE 环境中,如果未指定此元素,则可以假定默认值为 RESOURCE_LOCAL.

  • 为了确保 Java SE 应用程序的可移植性,必须明确列出持久性单元(JPA 规范)中包含的托管持久性类
<块引用>

我需要在运行时根据用户指定的值创建EntityManager

所以用这个:

Map addedOrOverridenProperties = new HashMap();//假设我们使用 Hibernate 作为 JPA 提供者addedOrOverridenProperties.put("hibernate.show_sql", true);Persistence.createEntityManagerFactory(, addedOrOverridenProperties);

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

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

解决方案

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

You should define at least one persistence unit in the persistence.xml deployment descriptor.

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

  • The name attribute is required. The other attributes and elements are optional. (JPA specification). So this should be more or less your minimal persistence.xml file:

<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.

So if your target Application Server supports JTA (JBoss, Websphere, GlassFish), your persistence.xml looks like:

<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>

If your target Application Server does not support JTA (Tomcat), your persistence.xml looks like:

<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>

If your data source is not bound to a global JNDI (for instance, outside a Java EE container), so you would usually define JPA provider, driver, url, user and password properties. But property name depends on the JPA provider. So, for Hibernate as JPA provider, your persistence.xml file will looks like:

<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>

Transaction Type Attribute

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.

  • To insure the portability of a Java SE application, it is necessary to explicitly list the managed persistence classes that are included in the persistence unit (JPA specification)

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

So use this:

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);

这篇关于创建没有persistence.xml配置文件的JPA EntityManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆