在Spring + Hibernate配置中获取EntityManager [英] Obtaining EntityManager in Spring + Hibernate configuration

查看:126
本文介绍了在Spring + Hibernate配置中获取EntityManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我可以按照这个教程。它工作正常,但我必须使用Hibernate的 Session 对象:

  @Autowired 
SessionFactory sessionFactory;

...

会话会话= sessionFactory.openSession();

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

  @PersistenceContext 
EntityManager entityManager;

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

  

然后,我遵循答案中的建议,并尝试使用以下代码:

  @PersistenceContext 
EntityManager entityManager;

...

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

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

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

现在我不需要任何Spring事务功能。我只想访问 EntityManager 并使用JPA。

Spring / Hibernate配置文件(hibernate.xml)

 <?xml version =1.0encoding =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 =dataSourceclass =org.apache.tomcat.dbcp.dbcp.BasicDataSource>
< property name =driverClassNamevalue =com.mysql.jdbc.Driver/>
< property name =urlvalue =jdbc:mysql:// localhost:3306 / test_db/>
< property name =usernamevalue =test/>
< property name =passwordvalue =test/>
< / bean>

< bean id =entityManagerFactoryclass =org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean>
< property name =dataSourceref =dataSource/>
< property name =packagesToScanvalue =net.myproject/>
< property name =jpaVendorAdapter>
< bean class =org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter/>
< / property>
< property name =jpaProperties>
<道具>
< prop key =hibernate.hbm2ddl.auto> update< / prop>
< prop key =hibernate.dialect> org.hibernate.dialect.MySQL5Dialect< / prop>
< prop key =hibernate.show_sql> true< / prop>
< /道具>
< / property>
< / bean>

< bean id =transactionManagerclass =org.springframework.orm.jpa.JpaTransactionManager>
< property name =entityManagerFactoryref =entityManagerFactory/>
< / bean>

< bean id =persistenceExceptionTranslationPostProcessorclass =org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor/>

< tx:annotation-driven />

< / beans>

我尝试使用的类 EntityManager

  @Repository 
public class ProductsService {

@ PersistenceContext
EntityManager entityManager;

@Transactional
public GridResponse< Product> getProducts(GridRequest dRequest){

//以下行导致异常:java.lang.IllegalStateException:无事务性EntityManager可用
Session session = entityManager.unwrap(Session.class) ;

// ...
}

...

$ b $对于 @PersistenceContext EntityManager entityManager; 方法,添加 tx:annotation -driven 添加到.xml配置中,并将您的方法标记为 entityManager 作为 @Transactional


I have a Spring MVC 4.0 application, and I am learning JPA. I use Hibernate as the JPA implementation.

I can configure Hibernate as described in this tutorial. It works fine, but I have to use Hibernate's Session object:

@Autowired
SessionFactory sessionFactory;

...

Session session = sessionFactory.openSession();

Now, I want to use JPA's EntityManager instead. I have followed this tutorial on the same web site (the configuration is very similar). And I tried to obtain an EntityManager object this way:

@PersistenceContext
EntityManager entityManager;

I got a runtime message:

java.lang.IllegalStateException: No transactional EntityManager available

Then, I followed the suggestion in this answer, 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.

What is the right way to get EntityManager in Spring + Hibernate configuration?

I do not need any Spring transaction functionality for now. I just want to get an access to EntityManager and play with JPA.

Spring/Hibernate configuration file (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

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

        //...
    }

...

解决方案

For the @PersistenceContext EntityManager entityManager; approach, add tx:annotation-driven to your .xml configuration and mark your methods where you use entityManager as @Transactional.

这篇关于在Spring + Hibernate配置中获取EntityManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆