jpa could not initialize proxy [com.modelname#1] - no Session 解决办法

jpa测试Dao.getOne的时候报错could not initialize proxy [com.modelname#1] - no Session

测试代如下

    @Test
    public void findOne_getOne() {
        System.out.println("=============== findPage ================" );
        SysUser entity=new SysUser();
        entity.setUserId(1);
       SysUser enitityNew=sysUserDao.getOne(entity.getUserId());
        if(enitityNew!=null){
            System.out.println("enitityNew ok:" );
            System.out.println("name:"+ enitityNew.getUserName());
        }else {
            System.out.println("enitityNew is null:" );
        }
    }

 

解决办法:给方法添加 @Transactional 注解就搞定了,如下

@Test
    @Transactional //不加这个会报错 could not initialize proxy [com.model#1] - no Session
    public void findOne_getOne() {
        System.out.println("=============== findPage ================" );
        SysUser entity=new SysUser();
        entity.setUserId(1);
       SysUser enitityNew=sysUserDao.getOne(entity.getUserId());
        if(enitityNew!=null){
            System.out.println("enitityNew ok:" );
            System.out.println("name:"+ enitityNew.getUserName());
        }else {
            System.out.println("enitityNew is null:" );
        }
    }

 

posted @ 2020-05-13 12:52  为乐而来  阅读(638)  评论(0编辑  收藏  举报