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:" ); } }
欢迎加入JAVA技术交流QQ群:179945282
欢迎加入ASP.NET(C#)交流QQ群:17534377