No entity found for query异常

错误为getSingleResult();获取值时获取不到报异常。

getSingleResult的源码有一句: @throws EntityNotFoundException if there is no result

查不到结果时,它是抛异常的,不会返回null.
解决方式,将异常捕捉
try{
Query query =this.getJpaTemplate().getEntityManager().createNativeQuery(sql,SmpRole.class);
obj = (SmpRole)query.getSingleResult();

    }catch(NoResultException e){
return null;
    }

posted @ 2017-03-23 14:09  Claricre  阅读(1852)  评论(0)    收藏  举报