Spring+Hibernate配置中获取EntityManager
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25260527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Obtaining EntityManager in Spring + Hibernate configuration
提问by Alex
I have a Spring MVC 4.0 application, and I am learning JPA. I use Hibernate as the JPA implementation.
我有一个 Spring MVC 4.0 应用程序,我正在学习 JPA。我使用 Hibernate 作为 JPA 实现。
I can configure Hibernate as described in thistutorial. It works fine, but I have to use Hibernate's Sessionobject:
我可以按照本教程中的描述配置 Hibernate 。它工作正常,但我必须使用 Hibernate 的Session对象:
@Autowired
SessionFactory sessionFactory;
...
Session session = sessionFactory.openSession();
Now, I want to use JPA's EntityManagerinstead. I have followed thistutorial on the same web site (the configuration is very similar). And I tried to obtain an EntityManagerobject this way:
现在,我想改用 JPA EntityManager。我在同一个网站上遵循了本教程(配置非常相似)。我试图通过EntityManager这种方式获得一个对象:
@PersistenceContext
EntityManager entityManager;
I got a runtime message:
我收到一条运行时消息:
java.lang.IllegalStateException: No transactional EntityManager available
Then, I followed the suggestion in thisanswer, and tried to use the following code:
然后,我遵循了这个答案中的建议,并尝试使用以下代码:
@PersistenceContext
EntityManager entityManager;
...
entityManager=entityManager.getEntityManagerFactory().createEntityManager();
It works a few times (about 9 repetitive method invocations), and then the application freezes.
它工作了几次(大约 9 次重复的方法调用),然后应用程序冻结。
What is the right way to get EntityManagerin Spring + Hibernate configuration?
EntityManager进入 Spring + Hibernate 配置的正确方法是什么?
I do not need any Spring transaction functionality for now. I just want to get an access to EntityManagerand play with JPA.
我现在不需要任何 Spring 事务功能。我只想访问EntityManager并使用 JPA。
Spring/Hibernate configuration file (hibernate.xml)
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>
The class where I attempt to use EntityManager
我尝试使用的课程 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);
//...
}
...
采纳答案by Andrei Stefan
For the @PersistenceContext EntityManager entityManager;approach, add tx:annotation-drivento your .xml configuration and mark your methods where you use entityManageras @Transactional.
对于该@PersistenceContext EntityManager entityManager;方法,添加tx:annotation-driven到您的 .xml 配置并将您使用的方法标记entityManager为@Transactional.
回答by B?ng Rikimaru
It can be use with @Autowired as shown in https://stackoverflow.com/a/33742769/2028440
它可以与 @Autowired 一起使用,如https://stackoverflow.com/a/33742769/2028440所示
@Autowired
private EntityManager entityManager;

