• jpa could not initialize proxy [com.modelname#1]


    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:" );
            }
        }
  • 相关阅读:
    文件管理后章
    文件管理前章
    文本处理三剑客
    匿名函数以及应用、三元表达式
    认识docker以及常用操作
    字符编码
    赋值与深浅拷贝
    Python基本运算符
    LVM配置
    Linux磁盘管理
  • 原文地址:https://www.cnblogs.com/q149072205/p/12881736.html
Copyright © 2020-2023  润新知