更新时间:2022-06-15 09:24:53
是否可以在未定义持久性单元的情况下初始化
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
?
persistence.xml
文件:
persistence.xml
file:<persistence>
<persistence-unit name="[REQUIRED_PERSISTENCE_UNIT_NAME_GOES_HERE]">
SOME_PROPERTIES
</persistence-unit>
</persistence>
在Java EE环境中,
jta-data-source
和non-jta-data-source
元素用于指定持久性提供程序要使用的JTA和/或非JTA数据源的全局JNDI名称
In Java EE environments, the
jta-data-source
andnon-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 ofRESOURCE_LOCAL
may be assumed.
我需要在运行时根据用户指定的值创建
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);