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中的事务管理器JPA DAO_Spring_Hibernate_Jpa - Fatal编程技术网

Spring中的事务管理器JPA DAO

Spring中的事务管理器JPA DAO,spring,hibernate,jpa,Spring,Hibernate,Jpa,我在spring和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:aop="http://www.springframework.org/schema/aop" xsi:sc

我在spring和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:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">


<bean id="entityManger"
    class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="JPA-01" />
</bean>

<bean id="dao" class="springdao.MessageDAO" />

</beans>
我有以下问题,

  • 我已经自动连接了持久性上下文。我想断言当前的持久上下文是否关联
  • 我没有使用applicationcontext.xml中配置的事务管理器。只需对类进行注释,我就能够将事务配置到服务
  • 我希望获得实体事务id,以便断言整个服务(涉及许多DAO)使用相同的事务 1) 在MessageDAO中为EntityManager提供一个getter方法,并使用assertNotNull(尽管不建议也不要求测试此场景;您应该只测试业务逻辑,并相信框架会正确关联EntityManager)

    2) 从数据库读取数据不需要事务。然而,将数据写入数据库确实需要事务。因此,您需要为持久性操作配置事务管理器,如下所示

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
    </bean>
    
    
    
    您可以通过两种方式使方法在事务下执行

    • XML配置
    • @事务注释
    在没有TransactionManager的情况下使用@Transactional注释您的方法将自动忽略它

    3) 我不知道如何检索事务id。
    然而,使用Spring这样的框架的主要目的是,有人已经为您测试了基础架构代码,您可以只专注于测试您的业务逻辑。尝试重新测试可能不是一个好主意。

    当您使用实体管理器作为数据访问API时,可以使用以下事务管理器配置

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="dataSource" ref="dataSource"/>
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>
    
    
    
    默认情况下,如果命名为“datasource”和“entityManagerFactory”,则此事务管理器将拾取数据源bean。此事务管理器变体将指定entitymanager工厂中的实体管理器与当前线程绑定。同样值得一看它的功能

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
    </bean>
    
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="dataSource" ref="dataSource"/>
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>