创建没有 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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您应该在
persistence.xml
部署描述符中定义至少一个持久性单元。persistence.xml
文件:因此,如果您的目标应用程序服务器支持 JTA(JBoss、Websphere、GlassFish),则您的 persistence.xml 如下所示:
如果您的目标应用程序服务器不支持 JTA (Tomcat),则您的 persistence.xml 看起来像:
如果您的数据源未绑定到全局 JNDI(例如,在 Java EE 容器之外),那么您通常会定义 JPA 提供程序、驱动程序、url、用户和密码属性。 但是属性名称取决于 JPA 提供商。因此,对于 Hibernate 作为 JPA 提供者,您的 persistence.xml 文件将如下所示:
事务类型属性
因此使用:
You should define at least one persistence unit in the
persistence.xml
deployment descriptor.persistence.xml
file:So if your target Application Server supports JTA (JBoss, Websphere, GlassFish), your
persistence.xml
looks like:If your target Application Server does not support JTA (Tomcat), your
persistence.xml
looks like: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:Transaction Type Attribute
So use this:
是的,您可以在@Configuration类(或其等效的spring配置xml)中使用spring,而无需使用任何xml文件:
Yes you can without using any xml file using spring like this inside a @Configuration class (or its equivalent spring config xml):
这是一个没有 Spring 的解决方案。
常量取自
org.hibernate.cfg.AvailableSettings
:以及臭名昭著的
PersistenceUnitInfo
Here's a solution without Spring.
Constants are taken from
org.hibernate.cfg.AvailableSettings
:And the infamous
PersistenceUnitInfo
我能够纯粹使用 Java 代码(带有 Spring 配置)使用 Hibernate 和 PostgreSQL 创建一个
EntityManager
,如下所示:对
LocalContainerEntityManagerFactoryBean.afterPropertiesSet()
的调用是 必不可少,否则工厂永远不会被构建,然后getObject()
返回null
并且你整天都在追逐NullPointerException
长的。 >:-(然后使用以下代码:
我的实体是这样的:
I was able to create an
EntityManager
with Hibernate and PostgreSQL purely using Java code (with a Spring configuration) the following:The call to
LocalContainerEntityManagerFactoryBean.afterPropertiesSet()
is essential since otherwise the factory never gets built, and thengetObject()
returnsnull
and you are chasing afterNullPointerException
s all day long. >:-(It then worked with the following code:
Where my entity was this:
对于普通的 JPA,假设您有一个
PersistenceProvider
实现(例如 Hibernate),您可以使用 PersistenceProvider#createContainerEntityManagerFactory(PersistenceUnitInfo info, Map map) 方法来引导EntityManagerFactory
,无需persistence.xml
。然而,令人烦恼的是,您必须实现 PersistenceUnitInfo 接口,因此您最好使用 Spring 或 Hibernate,它们都支持在没有
persistence.xml
文件的情况下引导 JPA: PersistenceUnitInfo 由 Spring 特定的实现MutablePersistenceUnitInfo 类。
With plain JPA, assuming that you have a
PersistenceProvider
implementation (e.g. Hibernate), you can use the PersistenceProvider#createContainerEntityManagerFactory(PersistenceUnitInfo info, Map map) method to bootstrap anEntityManagerFactory
without needing apersistence.xml
.However, it's annoying that you have to implement the
PersistenceUnitInfo
interface, so you are better off using Spring or Hibernate which both support bootstrapping JPA without apersistence.xml
file:Where the PersistenceUnitInfo is implemented by the Spring-specific MutablePersistenceUnitInfo class.
我使用的 DataNucleus JPA 也有一种方法 在其文档。不需要 Spring,也不需要丑陋的
PersistenceUnitInfo
实现。只需执行以下操作
DataNucleus JPA that I use also has a way of doing this in its docs. No need for Spring, or ugly implementation of
PersistenceUnitInfo
.Simply do as follows