在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。

<?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>

@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

以上是 在Spring + Hibernate配置中获取EntityManager 的全部内容, 来源链接: utcz.com/qa/409403.html

回到顶部