在 Spring Context 中创建 JPA EntityMananger 时出现问题
我有一个 JPA/Spring 应用程序,它使用 Hibernate 作为 JPA 提供程序。在代码的一部分中,我必须使用 new 运算符在应用程序中手动创建 DAO,而不是使用 Spring DI。当我这样做时,Spring 不会处理 @PersistenceContext 注释。
在我创建DAO的代码中,我有一个EntityManagerFactory,我用它来设置entityManager,如下所示:
@PersistenceUnit
private EntityManagerFactory entityManagerFactory;
MyDAO dao = new MyDAOImpl();
dao.setEntityManager(entityManagerFactory.createEntityManager());
问题是,当我这样做时,我收到一个Hibernate错误:
Could not find UserTransaction in JNDI [java:comp/UserTransaction]
这是spring实体管理器工厂配置:
<bean id="myAppTestLocalEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="myapp-core" />
<property name="persistenceUnitPostProcessors">
<bean class="com.myapp.core.persist.util.JtaPersistenceUnitPostProcessor">
<property name="jtaDataSource" ref="myappPersistTestJdbcDataSource" />
</bean>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</prop>
<prop key="hibernate.transaction.manager_lookup_class">com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup</prop>
</props>
</property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<!-- The following use the PropertyPlaceholderConfigurer but it doesn't work in Eclipse -->
<property name="database" value="$DS{hibernate.database}" />
<property name="databasePlatform" value="$DS{hibernate.dialect}" />
I have a JPA/Spring application that uses Hibernate as the JPA provider. In one portion of the code, I have to manually create a DAO in my application with the new operator rather than use Spring DI. When I do this, the @PersistenceContext annotation is not processed by Spring.
In my code where I create the DAO I have an EntityManagerFactory which I used to set the entityManager as follows:
@PersistenceUnit
private EntityManagerFactory entityManagerFactory;
MyDAO dao = new MyDAOImpl();
dao.setEntityManager(entityManagerFactory.createEntityManager());
The problem is that when I do this, I get a Hibernate error:
Could not find UserTransaction in JNDI [java:comp/UserTransaction]
Here's the spring entity manager factory configuration:
<bean id="myAppTestLocalEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="myapp-core" />
<property name="persistenceUnitPostProcessors">
<bean class="com.myapp.core.persist.util.JtaPersistenceUnitPostProcessor">
<property name="jtaDataSource" ref="myappPersistTestJdbcDataSource" />
</bean>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</prop>
<prop key="hibernate.transaction.manager_lookup_class">com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup</prop>
</props>
</property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<!-- The following use the PropertyPlaceholderConfigurer but it doesn't work in Eclipse -->
<property name="database" value="$DS{hibernate.database}" />
<property name="databasePlatform" value="$DS{hibernate.dialect}" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它与 EntityManager 无关。这是一个事务配置问题。显然 org.hibernate.transaction.JTATransactionFactory 强制进行 JNDI 查找。
切换到 com.atomikos.icatch.jta.hibernate3.AtomikosJTATransactionFactory ,一切正常。
It has nothing to do with the EntityManager. It is a transaction configuration problem. Apparently org.hibernate.transaction.JTATransactionFactory forces a JNDI lookup.
Switched to com.atomikos.icatch.jta.hibernate3.AtomikosJTATransactionFactory and everything works.