Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 在春季获得EntityManager+;休眠配置_Spring_Hibernate_Spring Mvc_Jpa - Fatal编程技术网

Spring 在春季获得EntityManager+;休眠配置

Spring 在春季获得EntityManager+;休眠配置,spring,hibernate,spring-mvc,jpa,Spring,Hibernate,Spring Mvc,Jpa,我有一个SpringMVC4.0应用程序,我正在学习JPA。我使用Hibernate作为JPA实现 我可以按照教程中的描述配置Hibernate。它工作正常,但我必须使用Hibernate的会话对象: @Autowired SessionFactory sessionFactory; ... Session session = sessionFactory.openSession(); 现在,我想改用JPA的EntityManager。我在同一个网站上学习了教程(配置非常相似)。我试图通过

我有一个SpringMVC4.0应用程序,我正在学习JPA。我使用Hibernate作为JPA实现

我可以按照教程中的描述配置Hibernate。它工作正常,但我必须使用Hibernate的
会话
对象:

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


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

它可以与@Autowired一起使用,如中所示


可能会帮助您。对于
@PersistenceContext EntityManager EntityManager方法,添加
tx:annotation-driven
,并将您使用entityManager的方法标记为
@Transactional
@Andrei感谢您的回复。在我以前的实验中,我已经尝试过这些东西,现在我又尝试了一次。我收到了相同的错误消息:
java.lang.IllegalStateException:没有可用的事务性EntityManager
。(我在没有
@Transactional
的情况下得到了相同的异常)。发布更多的源代码,比如您在哪里使用EntityManager(方法和类定义以及您可能在类上放置的任何注释)。它是一个网络应用程序吗?如果是这样,发布
web.xml
,并指定在哪个.xml文件中可以找到您已经发布的配置。您是否也可以发布
spring mvc servlet.xml
?此行:
Session Session=entityManager.unwrap(Session.class)
抛出异常,但我可以使用
entityManager
执行查询时不会出现问题。当我从HibernateAPI迁移到JPA时,我不再需要Hibernate的
会话
s,我可以完全删除该行。但是,我的代码只能在
就绪的情况下使用。因此,这个响应是正确的。另外一个技巧是在类的构造函数上调用
@Autowired
,以避免在每个属性上单独使用它。
<?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);

        //...
    }

...
@Autowired
private EntityManager entityManager;