在 Spring Context 中创建 JPA EntityMananger 时出现问题

发布于 2024-09-05 11:46:58 字数 1927 浏览 6 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小草泠泠 2024-09-12 11:46:59

它与 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文