在Spring + Hibernate配置中获取EntityManager

时间:2014-08-12 09:17:19

标签: spring hibernate spring-mvc jpa

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

我可以按this教程中的描述配置Hibernate。它运行正常,但我必须使用Hibernate的Session对象:

@Autowired
SessionFactory sessionFactory;

...

Session session = sessionFactory.openSession();

现在,我想使用JPA' EntityManager代替。我在同一个网站上关注了this教程(配置非常相似)。我尝试以这种方式获得EntityManager对象:

@PersistenceContext
EntityManager entityManager;

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

java.lang.IllegalStateException: No transactional EntityManager available

然后,我按照this回答中的建议,尝试使用以下代码:

@PersistenceContext
EntityManager entityManager;

...

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

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

在Spring + Hibernate配置中获取EntityManager的正确方法是什么?

我现在不需要任何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);

        //...
    }

...

2 个答案:

答案 0 :(得分:14)

对于@PersistenceContext EntityManager entityManager;方法,请将tx:annotation-driven添加到.xml配置中,并将entityManager用作@Transactional的方法标记为{{1}}。

答案 1 :(得分:3)

它可以与@Autowired一起使用,如https://stackoverflow.com/a/33742769/2028440

所示
@Autowired
private EntityManager entityManager;