在Spring + Hibernate配置中获取EntityManager


问题内容

我有一个Spring MVC 4.0应用程序,正在学习JPA。我使用Hibernate作为JPA实现。

我可以按照教程中的说明配置Hibernate
。它工作正常,但我必须使用Hibernate的Session对象:

@Autowired
SessionFactory sessionFactory;

...

Session session = sessionFactory.openSession();

现在,我想改用JPA
EntityManager。我在同一个网站上遵循了教程(配置非常相似)。我试图通过EntityManager这种方式获取对象:

@PersistenceContext
EntityManager entityManager;

我收到一条运行时消息:

java.lang.IllegalStateException: No transactional EntityManager available

然后,我遵循了答案中的建议,并尝试使用以下代码:

@PersistenceContext
EntityManager entityManager;

...

entityManager=entityManager.getEntityManagerFactory().createEntityManager();

它可以工作几次(大约9次重复的方法调用),然后应用程序冻结。

什么是获得EntityManagerSpring + Hibernate配置的正确方法?

我现在不需要任何Spring交易功能。我只想访问EntityManager并使用JPA。

Spring / Hibernate配置文件(hibernate.xml)

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/test_db" />
        <property name="username" value="test" />
        <property name="password" value="test" />
    </bean>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="net.myproject" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <bean id="persistenceExceptionTranslationPostProcessor" class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

    <tx:annotation-driven />

</beans>

我尝试使用的班级EntityManager

@Repository
public class ProductsService {

    @PersistenceContext
    EntityManager entityManager;

    @Transactional
    public GridResponse<Product> getProducts(GridRequest dRequest) {

        // The following line causes the exception: "java.lang.IllegalStateException: No transactional EntityManager available"
        Session session = entityManager.unwrap(Session.class);

        //...
    }

...

问题答案:

对于此@PersistenceContext EntityManager entityManager;方法,请添加tx:annotation- driven到.xml配置中,并将您使用的方法标记entityManager@Transactional