在Spring上下文中创建JPA EntityMananger时出现问题 [英] Problem creating JPA EntityMananger in Spring Context

查看:182
本文介绍了在Spring上下文中创建JPA EntityMananger时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Hibernate作为JPA提供者的JPA / Spring应用程序。在代码的一部分中,我必须使用新的运算符在应用程序中手动创建DAO,而不是使用Spring DI。当我这样做时,@PersistenceContext注解不会被Spring处理。

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.

在我创建DAO的代码中,我有一个EntityManagerFactory,它用于将entityManager设置为如下:

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());

问题是,当我这样做时,我得到一个Hibernate错误:

The problem is that when I do this, I get a Hibernate error:

Could not find UserTransaction in JNDI [java:comp/UserTransaction]

以下是Spring实体管理器工厂配置:

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}" />





推荐答案

它与EntityManager无关。这是一个交易配置问题。显然org.hibernate.transaction.JTATransactionFactory强制进行JNDI查找。

It has nothing to do with the EntityManager. It is a transaction configuration problem. Apparently org.hibernate.transaction.JTATransactionFactory forces a JNDI lookup.

切换到com.atomikos.icatch.jta.hibernate3.AtomikosJTATransactionFactory,一切正常。

Switched to com.atomikos.icatch.jta.hibernate3.AtomikosJTATransactionFactory and everything works.

这篇关于在Spring上下文中创建JPA EntityMananger时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆