我正在使用 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()
- 这就是为什么我怀疑事情不工作。如果可以避免,我宁愿不使用这种方法,因为生成的代码看起来很糟糕。
我的休眠 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 />
我的测试代码看起来像这样(我必须将其展平,因为我的测试是从一个公共父测试类扩展而来的):
@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));
}
...
谢谢您的帮助!