Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.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
Java 未找到当前线程、Spring 3.1和;冬眠4_Java_Spring_Hibernate - Fatal编程技术网

Java 未找到当前线程、Spring 3.1和;冬眠4

Java 未找到当前线程、Spring 3.1和;冬眠4,java,spring,hibernate,Java,Spring,Hibernate,我试图在一个演示独立应用程序中使用Spring,使用DAO层和服务层,使用委派的Hibernate事务 我已经正确地设置了配置,并且对DAO方法使用@Transactional注释进行了单元测试,效果良好,但是当我将此注释移动到服务层时,我得到一个: org.hibernate.HibernateException:未找到当前线程的会话 我提供了代码中最相关的部分,希望您能给我一个提示,让我理解这里发生了什么 applicationContext.xml 类路径:com/genericdao

我试图在一个演示独立应用程序中使用Spring,使用DAO层和服务层,使用委派的Hibernate事务

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

org.hibernate.HibernateException:未找到当前线程的会话
我提供了代码中最相关的部分,希望您能给我一个提示,让我理解这里发生了什么

applicationContext.xml

类路径:com/genericdao/hbm
org.hibernate.dialogue.sybaseanywhere方言
真的
真的
org.springframework.orm.hibernate4.SpringSessionContext
... 我在这里提供配置
道层 getSession()只执行sessionFactory.getCurrentSession() sessionFactory在GenericDaoHibernateImpl中自动连接

@Repository(“userLoginDao”)
公共类UserLoginDaoImpl扩展了GenericDaoHibernateImpl实现UserLoginDao{
@凌驾
//@Transactional(readOnly=true)//当我进行单元测试时,这个功能就起作用了!!但是我不想在这里使用@Transactional!!
公共列表findAll(){
布尔active=TransactionSynchronizationManager.isActualTransactionActive();//如果使用@Transactional,则始终为true
Query Query=getSession().createQuery(“来自UserLogin”);
return(List)query.List();
}                  
}
服务层
@Service(“pruebaService”)
公共类PruebaServiceImpl实现PruebaService{
专用静态最终应用程序上下文ctx;
静止的{
ctx=新类路径XmlApplicationContext(“applicationContext.xml”);
}   
/**********************************************************
*这里我想使用@Transactional,但它不起作用,因为
*我得到org.hibernate.HibernateException:找不到当前线程的会话
*当我测试此方法的调用时。。。
*注意:我想声明,如果我取消注释Dao@Transactional行
*这样就行了!!,但正如我前面提到的,我真的不想在DAO方法上有事务,只想在服务方法上有事务!!
*/
@凌驾
@事务(只读=真)
公共列表obtenerTodasLasCuentas(){
userlogindaobean=(UserLoginDao)ctx.getBean(“UserLoginDao”);
List result=bean.findAll();
返回结果;
}
}
我真的在这个话题上做了一个探讨,但我找不到合适的输入。。。希望你能帮上忙。。。谢谢

更新: 下面是我正在使用的测试相关代码

测试代码
@测试
公共void selectTest(){
pruebaService=(pruebaService)ctx.getBean(“pruebaService”);
Assert.assertNotNull(pruebaService);//这个断言很好,所以我知道服务被正确注入
列表结果=pruebaService.obtenerTodasLasCuentas();
Assert.assertNotNull(result);//由于我提到的异常,从未到达此行!!
}

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


如果你还想指出什么。。。我非常感谢你的评论。。。非常感谢你们的帮助,伙计们

服务类的套餐是什么?@SotiriosDelimanolis可能是对的。如果Spring没有构造您的
PruebaServiceImpl
类,那么它将看不到
@Transactional
注释,也无法创建必要的方面。@SotiriosDelimanolis@Pace服务位于
com.genericdao.Service
上,因此它实际上是被创建的,它看到的是
@Transactional
,因为当我测试
TransactionSynchronizationManager.isActualTransactionActive()
(DAO findAll方法)时,它的计算结果总是正确的。请告诉我们您在哪里注入或使用服务类。我认为这是因为base package=“com.genericdao”。
@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();
    }                  
}
@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!!
}
@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");