0

我正在尝试在演示独立应用程序中使用 Spring 委托的 Hibernate 事务,使用DAO 层Service 层

我已经正确设置了配置,并且我已经对DAO方法上@Transactional注释的使用进行了单元测试,但是当我将此注释移动到服务层时,我得到:

org.hibernate.HibernateException: No Session found for current thread

我提供了我的代码中最相关的部分,希望你能给我一个提示以了解这里发生了什么。

应用程序上下文.xml

<beans ...>    
    <context:component-scan base-package="com.genericdao" />           

    <!-- delegat transactions -->
    <bean id="transactionManager"
          class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />

    <!-- sessionFactory config -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>        
        <property name="mappingDirectoryLocations">
            <list>  
                 <value>classpath:com/genericdao/hbm</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.SybaseAnywhereDialect</prop>
                <!--prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop--><!-- i tried commenting this line -->                
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>                    
                <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop><!-- i know this is provided by default -->
            </props>
        </property>
    </bean>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        ... i provide here configuration
    </bean>                  
</beans>

道层

getSession()只是执行sessionFactory.getCurrentSession(); sessionFactoryGenericDaoHibernateImpl

@Repository("userLoginDao")
public class UserLoginDaoImpl extends GenericDaoHibernateImpl<UserLogin, Integer> implements UserLoginDao{            

    @Override
    //@Transactional(readOnly=true) // This works when i unit-test!! But I don't want to use @Transactional here!!
    public List<UserLogin> findAll() {        
        boolean active = TransactionSynchronizationManager.isActualTransactionActive(); // always true if i use @Transactional

        Query query = getSession().createQuery("from UserLogin");

        return (List<UserLogin>) query.list();
    }                  
}

服务层

@Service("pruebaService")
public class PruebaServiceImpl implements PruebaService{

    private static final ApplicationContext ctx;

    static{
        ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
    }   

    /**********************************************************
     * HERE i want to use @Transactional, but it doesn't work because
     * i get the org.hibernate.HibernateException: No Session found for current thread
     * when i test invocation of this method...
     * NOTE: I want to state, that if i uncomment the Dao @Transactional line
     * then this works!!, but as i mentioned before i really don't want to have transactions on DAO methods, just Service Methods!!
     */
    @Override
    @Transactional(readOnly=true) 
    public List<UserLogin> obtenerTodasLasCuentas() {
        UserLoginDao bean = (UserLoginDao) ctx.getBean("userLoginDao");
        List<UserLogin> result = bean.findAll();
        return result;
    }
}

我真的对这个主题做了一个搜索,但我找不到合适的输入......希望你能帮忙......谢谢。

更新:

这是我正在使用的测试相关代码

测试代码

@Test
public void selectTest(){
    pruebaService = (PruebaService) ctx.getBean("pruebaService");            
    Assert.assertNotNull(pruebaService); // This assert is good, so i know service is properly injected            
    List<UserLogin> result = pruebaService.obtenerTodasLasCuentas();            
    Assert.assertNotNull(result); // This line is never reached because of the exception i mentioned!!
}
4

1 回答 1

0

好的,我完成了这项工作,我只是将服务修改为:

@Service("pruebaService")
public class PruebaServiceImpl implements PruebaService{

    private @Autowired UserLoginDao userLoginDao; // injected by Spring when I create the ApplicationContext in my unit-testing class


    @Override
    @Transactional(readOnly=true) 
    public List<UserLogin> obtenerTodasLasCuentas() {        
        List<UserLogin> result = userLoginDao.findAll();
        return result;
    }
}

这是因为误导了两次创建(一次在我的服务中,一次在我的单元测试课程中):

new ClassPathXmlApplicationContext("applicationContext.xml");

如果您还有其他要指出的...我非常感谢您的评论...非常感谢您的帮助

于 2013-11-08T17:12:42.707 回答