简体   繁体   English

从Persistence API使用EntityManagerFactory

[英]Using EntityManagerFactory from Persistence API

I would like to get the EntityManagerFactory instance from Persistence interface in a jee environment.. but the specification link : https://docs.oracle.com/javaee/7/api/javax/persistence/Persistence.html 我想在jee环境中从Persistence接口获取EntityManagerFactory实例..但规范链接为: https : //docs.oracle.com/javaee/7/api/javax/persistence/Persistence.html

states the following 陈述以下

The Persistence class is available in a Java EE container environment as well; Persistence类在Java EE容器环境中也可用。 however, support for the Java SE bootstrapping APIs is not required in container environments. 但是,在容器环境中不需要支持Java SE自举API。

Does this mean that we should not be using Persistence.createEntityManagerFactory not to be used in a java EE environment ? 这是否意味着我们不应该在Java EE环境中不使用Persistence.createEntityManagerFactory?

Note: I have tested at WAS 8.5.5 and JBOSS EAP 7 and able to successfully get the EntityManagerFactory and perform DB persistence. 注意:我已经在WAS 8.5.5和JBOSS EAP 7上进行了测试,并且能够成功获取EntityManagerFactory并执行数据库持久性。 I wanted to know if as per specification the container may stop the support for this API ? 我想知道容器是否按照规范会停止对此API的支持?

If I understand your comments correctly, your problem is that the data source is dynamic to some degree and therefore you want to use the bootstrapping API to create your EntityManagerFactory instead of configuring it "the EE way" through your persistence.xml 如果我正确理解了您的评论,那么您的问题是数据源在某种程度上是动态的,因此您想使用引导API创建EntityManagerFactory,而不是通过persistence.xml以“ EE方式”配置它。

How dynamic is your datasource exactly? 您的数据源到底有多动态? What EE container are you running? 您正在运行哪个EE容器? You might be able to configure the datasource in your container configuration (ie, on the server) and have your entitymanagerfactory either created by the container or automatically pick up the datasource through JNDI. 您可能能够在容器配置中(即在服务器上)配置数据源,并让您的实体管理工厂由容器创建或通过JNDI自动选择数据源。

At a former employer we ran Wildfly and we configured the datasource in the wildfly configuration on every server, letting wildfly provide the datasource on a standard location for hibernate to pick up and construct the EMF. 在以前的雇主中,我们运行Wildfly,然后在每台服务器上以wildfly配置配置数据源,让wildfly在标准位置提供数据源,以便休眠模式来获取和构造EMF。

See https://stackoverflow.com/a/41550908/691074 for an example of a similar setup. 有关类似设置的示例,请参见https://stackoverflow.com/a/41550908/691074

This enabled us to run the same code on different servers connecting to different databases (ie, test, production, etc.) 这使我们能够在连接到不同数据库(即测试,生产等)的不同服务器上运行相同的代码。

The default wildfly configuration already configures an in-memory H2 database, you could modify that to point in the correct direction. 默认的wildfly配置已经配置了内存中的H2数据库,您可以对其进行修改以指向正确的方向。

Here are the Wildfly 10 docs regarding datasource configuration and here is an explanation of the persistence.xml with examples for referencing a datasource managed by the container. 是有关数据源配置的Wildfly 10文档, 是对persistence.xml的解释,并提供了引用由容器管理的数据源的示例。

Would this work for your case? 这对您的情况有用吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法配置EntityManagerFactory(无持久性) - Unable to configure EntityManagerFactory (No Persistence) 使用Spring 4未定义类型为[javax.persistence.EntityManagerFactory]的合格Bean - No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined Using Spring 4 如何使用相同的持久性单元使用 EntityManager 和 EntityManagerFactory 访问具有相同实体的不同数据源 - How to use same Persistence Unit to access different datasources with the same entities using EntityManager and EntityManagerFactory 创建名称为'entityManagerFactory'的bean时出错->无法从类路径资源[META-INF / persistence.xml]中解析持久性单元 - Error creating bean with name 'entityManagerFactory' -> Cannot parse persistence unit from class path resource [META-INF/persistence.xml] 实体管理器工厂上的Spring3 JPA持久性异常 - Spring3 JPA Persistence Exception on entityManagerFactory 找不到Spring STS javax / persistence / EntityManagerFactory - Spring STS javax/persistence/EntityManagerFactory can not found 类型 jakarta.persistence.EntityManagerFactory 不存在 - Type jakarta.persistence.EntityManagerFactory not present javax.persistence.PersistenceException-无法构建EntityManagerFactory - javax.persistence.PersistenceException - Unable to build EntityManagerFactory 多个没有persistence.xml的entityManagerFactory - Multiple entityManagerFactory without persistence.xml 查找当前EntityManagerFactory的当前持久性单元 - Finding the current persistence unit for the current EntityManagerFactory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM