在Spring + Hibernate配置中获取EntityManager

Ale*_*lex 17 spring hibernate jpa spring-mvc

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

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

@Autowired
SessionFactory sessionFactory;

...

Session session = sessionFactory.openSession();
Run Code Online (Sandbox Code Playgroud)

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

@PersistenceContext
EntityManager entityManager;
Run Code Online (Sandbox Code Playgroud)

我收到了一条运行时消息:

java.lang.IllegalStateException: No transactional EntityManager available
Run Code Online (Sandbox Code Playgroud)

然后,我按照这个答案中的建议,并尝试使用以下代码:

@PersistenceContext
EntityManager entityManager;

...

entityManager=entityManager.getEntityManagerFactory().createEntityManager();
Run Code Online (Sandbox Code Playgroud)

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

EntityManager进入Spring + 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>
Run Code Online (Sandbox Code Playgroud)

我尝试使用的类 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);

        //...
    }

...
Run Code Online (Sandbox Code Playgroud)

And*_*fan 15

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