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

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

问题描述

是否可以在未定义持久性单元的情况下初始化EntityManager?您可以提供所有必需的属性来创建实体管理器吗?我需要在运行时根据用户的指定值创建EntityManager.无法更新persistence.xml并重新编译.

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!

推荐答案

是否可以在未定义持久性单元的情况下初始化EntityManager?

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

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

您可以提供创建Entitymanager的所有必需属性吗?

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

  • 名称属性是必需的.其他属性和元素是可选的. (JPA规范).因此,这应该或多或少是您最小的persistence.xml文件:
    • 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>
      

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

      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如下:

      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>
      

      如果目标应用服务器不支持JTA(Tomcat),则persistence.xml如下:

      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>
      

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

      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>
      

      交易类型属性

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

      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规范)中包含的托管持久性类
        • 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)
        • 我需要在运行时根据用户指定的值创建EntityManager

          所以使用这个:

          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天全站免登陆