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;
}
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;
}
本文介绍了JPA中getSingleResult方法的行为特点,当查询没有结果时会抛出EntityNotFoundException异常而不是返回null。文章提供了一种解决方案,通过try-catch结构来捕获NoResultException异常并返回null。
2169

被折叠的 条评论
为什么被折叠?



