簡體   English   中英

在Spring + Hibernate配置中獲取EntityManager

[英]Obtaining EntityManager in Spring + Hibernate configuration

我有一個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次重復的方法調用),然后應用程序凍結。

在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);

        //...
    }

...

對於@PersistenceContext EntityManager entityManager; 方法,將tx:annotation-driven添加到.xml配置中,並將使用entityManager方法標記為@Transactional

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

@Autowired
private EntityManager entityManager;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM