java – 在Spring事务JUnit测试中自动连接Hibernate Session的正
发布时间:2020-12-15 00:08:13 所属栏目:Java 来源:网络整理
导读:这个问题类似于之前的 one.我试图@Autowire一个Hibernate Session在我的一个Spring-JUnit事务测试中,但是我得到这个异常: java.lang.IllegalStateException:没有Hibernate会话绑定到线程,并且配置不允许创建非事务性 这是我的JUnit类: @RunWith(SpringJUn
这个问题类似于之前的
one.我试图@Autowire一个Hibernate Session在我的一个Spring-JUnit事务测试中,但是我得到这个异常:
java.lang.IllegalStateException:没有Hibernate会话绑定到线程,并且配置不允许创建非事务性 这是我的JUnit类: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"/applicationContext.xml"}) @TransactionConfiguration(transactionManager="transactionManager") @Transactional public class MyTest { @Qualifier("session") @Autowired private Session session; @Test public void testSomething() { session.get(User.class,"me@here.com"); } } 如果我@Autowire一个SessionFactory并以编程方式获取我的会话(而不是在Spring XML中定义它),每一个都可以正常工作: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"/applicationContext.xml"}) @TransactionConfiguration(transactionManager="transactionManager") @Transactional public class MyTest{ @Qualifier("sessionFactory") @Autowired private SessionFactory sessionFactory; @Test public void testSomething() { Session session = SessionFactoryUtils.getSession(sessionFactory,false); session.get(User.class,"me@here.com"); } } 但是,如果我使用< aop:scoped-proxy />在我的Spring 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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd "> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> ... </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation"><value>classpath:/hibernate.cfg.xml</value></property> <property name="configurationClass"> <value>org.hibernate.cfg.AnnotationConfiguration</value> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> <property name="dataSource" ref="dataSource" /> </bean> <tx:annotation-driven transaction-manager="transactionManager"/> <bean id="session" class="org.springframework.orm.hibernate3.SessionFactoryUtils" factory-method="getSession" scope="prototype"> <constructor-arg ref="sessionFactory" /> <constructor-arg value="false" /> <!-- This is seems to be needed to get rid of the 'No Hibernate Session' error' --> <aop:scoped-proxy /> </bean> </beans> 我的问题是:为什么< aop:scoped-proxy />需要考虑到在单元测试中应该只有一个线程有限的事务上下文?什么是定义我的Hibernate Session bean的正确方法? 解决方法
SessionFactoryUtils.getSession()与获取会话的任何其他方式一样好.它做同样的事情HibernateDaoSupport.getSession()会做.
您需要作用域代理的原因是因为时序.没有作用域代理,它似乎是在测试开始之前注入会话,因此在事务开始之前,所以你得到错误. 通过添加范围代理,它代理会话并注入,因此它不会在事务开始前先注入实际会话,而只是在测试运行之后提取它并调用它,当它实际需要做一个反对它. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- java – 无等待和无锁算法的示例/插图
- java – Runnable.toString()什么时候可以返回重复的String
- java – 在Spring MVC REST中返回文件
- 智能卡 – 恩智浦有Java卡吗?
- java – 为什么Collection.toArray(T [])不采用E []
- 让java显示超过1 .add()
- java.lang.IllegalArgumentException:观察者为null
- Java List.clear()方法:从列表中移除所有元素
- SpringMVC请求的路径变量里面写正则表达式的方法
- 解析Java反射java.lang.IllegalArgumentException: wrong n