我使用 Spring 3.1 和 Hibernate 4.1,当前在运行存储库测试时遇到以下异常:
Caused by: org.hibernate.HibernateException: No Session found for current thread
at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1041)
这是 DAO 的样子。它使用基本上其他人都在使用的相同泛型 dao 模式:
@Repository
public class UserRepository extends RepositorySupport<User> {
public UserRepository() {
super(User.class);
}
public User findByUsername(final String username) {
Query query = getCurrentSession().createQuery(
"from User user " +
"where user.username = :username"
);
return (User) query.setParameter("username", username)
.uniqueResult();
}
这里唯一不同的是我通常不会做的是我不再使用 getHibernateTemplate()
- 这就是为什么我怀疑事情不起作用。如果可以避免的话,我宁愿不使用这种方法,因为生成的代码看起来很糟糕。
我的 hibernate bean 看起来像这样:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>hibernate/tag.hbm.xml</value>
<value>hibernate/address.hbm.xml</value>
<value>hibernate/user.hbm.xml</value>
</list>
</property>
<property name="namingStrategy">
<bean class="org.hibernate.cfg.ImprovedNamingStrategy"/>
</property>
<property name="hibernateProperties">
<props>
<prop key="javax.persistence.validation.mode">none</prop>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}
</prop>
<prop key="hibernate.generate_statistics">false</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.cache.use_second_level_cache">false</prop>
<prop key="hibernate.cache.provider_class">
</prop>
</props>
</property>
</bean>
<bean class="org.springframework.orm.hibernate4.HibernateExceptionTranslator"/>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!--
<bean id="hibernateInterceptor"
class="org.springframework.orm.hibernate4.HibernateInterceptor">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
-->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="find*" read-only="true" propagation="REQUIRED"/>
<tx:method name="get*" read-only="true" propagation="REQUIRED"/>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:advisor advice-ref="transactionAdvice"
pointcut="execution(* *..service.*.*(..))" order="1"/>
<!-- gets sub packages like service.user -->
<aop:advisor advice-ref="transactionAdvice"
pointcut="execution(* *..service.*.*.*(..))" order="2"/>
</aop:config>
<bean id="validator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
<context:annotation-config/>
<context:spring-configured/>
<context:component-scan base-package="com.realtywebsite.repositories,com.realtywebsite.service"/>
<task:annotation-driven />
我的测试代码看起来像这样(我必须将其展平,因为我的测试从公共(public)父测试类扩展):
@ContextConfiguration(locations = {
ObjectMother.APPLICATION_CONTEXT,
ObjectMother.WEB_CONTEXT
})
public abstract class DatabaseTest extends AbstractTransactionalJUnit4SpringContextTests {
@Test
public void delete() {
userRepository.delete(1);
flush();
assertFalse(userRepository.exists(1));
}
...
感谢您的帮助!
最佳答案
AbstractTransactionalJUnit4SpringContextTests
类已弃用。
你有两个选择:
- 从
AbstractTransactionalJUnit4SpringContextTests
扩展,或 - 使用
SpringJUnit4ClassRunner
(然后需要附加@Transactional
)
我希望这能解决问题。
关于java - Spring 3.1 和 Hibernate 4 : "HibernateException: No Session found for current thread" When calling DAO method from unit test,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12808388/