Problem of creating JPA EntityManager in Spring Context

I have a JPA / Spring application that uses Hibernate as a JPA provider. In one piece of code, I need to manually create a DAO in my application with a new statement, and not 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 that 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:

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

         

+3
source share
1 answer

EntityManager. . , org.hibernate.transaction.JTATransactionFactory JNDI.

com.atomikos.icatch.jta.hibernate3.AtomikosJTATransactionFactory .

+3

Source: https://habr.com/ru/post/1749635/


All Articles