错误为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;
}