其他分享
首页 > 其他分享> > jpa could not initialize proxy [com.modelname#1] - no Session 解决办法

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:" );
        }
    }

 

标签:enitityNew,modelname,no,jpa,System,entity,SysUser,println,out
来源: https://www.cnblogs.com/q149072205/p/12881736.html