为什么在JPA EntityManager中查询会抛出NoResultException但找不到? [英] Why in JPA EntityManager queries throw NoResultException but find does not?

查看:203
本文介绍了为什么在JPA EntityManager中查询会抛出NoResultException但找不到?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我为什么在JPA 1.0 EntityManager中通过find检索对象时,必须处理null(如果找不到),但是当通过createQuery使用Query接口时,如果未找到,则抛出NoResultException. /p>

也许我遗漏了一些东西,但是我觉得它与一种语言非常不一致,实际上,由于使用查询界面从简单的查找程序更改为更细粒度的查询,因此我不得不进行大量的重新操作.

谢谢大家.

解决方案

查询可用于检索几乎所有内容,包括单行中单列的值.

如果getSingleResult()返回null,则无法确定查询是否与任何行都不匹配,或者查询是否与某行匹配,但所选列包含null作为其值.

Can somebody tell me the intrinsic reasons why in the JPA 1.0 EntityManager when retrieving an Object via find, you have to deal with null if not found, but when using the Query interface via createQuery getResultList throws a NoResultException when not found.

Maybe i am missing something but I feel its very inconsistent for a Language, and actually I had to do a lot of redesing because of changing from a simple finder to a more fine grained query using the query interface.

Thanks guys.

解决方案

Queries can be used to retrieve almost anything including the value of a single column in a single row.

If getSingleResult() would return null, you could not tell whether the query did not match any row or whether the query matched a row but the selected column contains null as its value.

这篇关于为什么在JPA EntityManager中查询会抛出NoResultException但找不到?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆