如何在不是来自JNDI的JPA EntityMangerFactory上设置数据源 [英] How to set a DataSource on a JPA EntityMangerFactory that is not from JNDI
问题描述
我对JPA还是很陌生,曾经使用过JDO(DataNucleus)和Hibernate.
I'm pretty new to JPA, having used JDO (DataNucleus) and Hibernate.
我了解了如何为JPA配置设置 persistence.xml
,但是我需要进行一些调整.我不想在XML中指定 DataSource
,而是要向 EntityManagerFactory
提供实际的 DataSource
对象.我之所以需要这样做,是因为我创建并管理了自己的 DataSource
对象,并且不依赖容器来执行此操作,因此我无法(也不想)查找 DataSource
通过 persistence.xml
中的JNDI名称.
I get how to set up persistence.xml
for the JPA configuration, but I need to make one tweak. Instead of specifying the DataSource
in the XML, I want to provide the actual DataSource
object to the EntityManagerFactory
. I need to do this because I create and manage my own DataSource
objects and do not rely on the container to do so, thus I cannot (and do not want to) look up the DataSource
via a JNDI name in persistence.xml
.
那么,如何为 EntityManagerFactory
提供一个 DataSource
对象,而不是在 persistence.xml
中指定它?我无法想象这很难很难,但是我似乎找不到它,而且我到处都是.
So, how to I provide a DataSource
object to the EntityManagerFactory
rather than specifying it in persistence.xml
? I can't imagine it's hard to do but I can't seem to find it, and I've looked all over the place.
如果有什么帮助,我将使用Hibernate 4作为JPA提供程序(实际上,我是从3.6过渡到4,其中 Ejb3Configuration
类消失了).希望我可以采用非Hibernate特定的方式来完成此操作,但是如果我必须使用Hibernate特定的API,这并没有什么大不了的.
If it helps at all, I'm using Hibernate 4 as the JPA provider (actually, I'm transitioning from 3.6 to 4, where the Ejb3Configuration
class is gone). Hopefully I can do it in a non-Hibernate specific way, but it's not a huge deal if I have to use Hibernate specific APIs.
谢谢!
-瑞安
推荐答案
我自己也想不出办法,所以我用Spring来做.这并不一定很糟,除了它需要我进口六个以前不打算使用的罐子外.无论如何,我最终还是将它们用于其他用途,但对于JPA而言,这是一个真正的失败,它甚至不能使用API来设置DataSource,而必须依赖于容器提供的DataSource.
I couldn't figure out any way to do it myself so I used Spring to do it. That's not necessarily bad, except that it required me to import a half dozen or so jars that I previously wasn't intending to use. I ended up using them for other things anyway, but still, I think it's a real failure on the part of JPA that one can't even use the API to set a DataSource but must instead rely on a container-provided DataSource.
我用Spring的 LocalContainerEntityManagerFactoryBean
来完成这项工作.它从 persistence.xml
中读取配置数据,然后我通过Spring API设置 DataSource
.Spring使用 DataSource
覆盖 persistence.xml
中定义的内容.
I used Spring's LocalContainerEntityManagerFactoryBean
to do the work. It reads the configuration data from persistence.xml
and I then set the DataSource
via the Spring API. Spring uses the DataSource
to override what was defined in persistence.xml
.
这是代码的样子.请注意对 afterPropertiesSet
方法的调用.这是必需的,因为我的应用程序不将Spring用于依赖项注入或AOP,而是将Guice用于这些任务.如果您不调用 afterPropertiesSet
方法,则对 getNativeEntityManagerManagerFactory
的调用将返回null.
Here's what the code looks like. Note the call to the afterPropertiesSet
method. This is required because my application does not use Spring for dependency injection or AOP, but instead uses Guice for those tasks. If you don't call the afterPropertiesSet
method then the call to getNativeEntityManagerFactory
returns null.
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
factoryBean.setDataSource(dataSource);
factoryBean.setPersistenceUnitName("persistenceUnitName");
factoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
factoryBean.afterPropertiesSet();
EntityManagerFactory factory = factoryBean.getNativeEntityManagerFactory();
这篇关于如何在不是来自JNDI的JPA EntityMangerFactory上设置数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!