我有一个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}" />
答案 0 :(得分:4)
它与EntityManager无关。这是一个事务配置问题。显然org.hibernate.transaction.JTATransactionFactory强制进行JNDI查找。
切换到com.atomikos.icatch.jta.hibernate3.AtomikosJTATransactionFactory,一切正常。