javax.persistence.NoResultException: getSingleResult() 没有检索到任何实体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2613113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javax.persistence.NoResultException: getSingleResult() did not retrieve any entities
提问by
i have created a namedquery with ejb to check if the username is used. When the singleResult is null, then i get the following Exception :
我用 ejb 创建了一个命名查询来检查是否使用了用户名。当 singleResult 为空时,我得到以下异常:
javax.persistence.NoResultException: getSingleResult() did not retrieve any entities
But this exception is the result that i want when the username is free.
但是这个异常是我想要的用户名空闲时的结果。
Here is the code:
这是代码:
public User getUserByUsername(String username) throws DAOException{
try{
Query q = em.createNamedQuery(User.getUserByUsername);
q.setParameter("username", username);
return (User) q.getSingleResult();
}catch(Exception e){
throwException(username, e);
return null;
}
}
Does anybody know what the problem is. :(
有谁知道是什么问题。:(
I would like to return null and don`t get an Exception.
我想返回 null 并且不会得到异常。
Thank you very much
非常感谢
回答by stefanglase
You seem to rethrow the exception in your catch block with the statement throwException(username, e);
. If you expect to get the user or null
without any exception this should look like the following:
您似乎使用语句在 catch 块中重新抛出异常throwException(username, e);
。如果您希望获得用户或null
没有任何例外,这应该如下所示:
public User getUserByUsernameOrNull(String username) {
try{
Query q = em.createNamedQuery(User.getUserByUsername);
q.setParameter("username", username);
return (User) q.getSingleResult();
} catch(NoResultException e) {
return null;
}
}
回答by JRL
What does the method throwException
do?
方法有throwException
什么作用?
Is an exception being thrown within it but you are using the prior exception's message?
是否在其中抛出了异常,但您正在使用先前的异常消息?
回答by ewernli
Use getResultList
instead and check if the List
is empty (has zero element). Otherwise, the list contains one element and you simply return it.
使用getResultList
替代,并检查List
为空(零元)。否则,列表包含一个元素,您只需返回它。
回答by Michael Konietzka
You experience the defined behaviour when calling getSingleResultand none entry was found: A NoResultExceptionis thrown. You can catch NoResultException in the catch-clause, because the transaction won't be marked as rollback, when JPA is throwing NoResultException. Or you can use getResultList() and check if size is exactly "1", so you know you have found your user.
您在调用getSingleResult时遇到定义的行为,但未找到任何条目:抛出NoResultException。您可以在 catch 子句中捕获 NoResultException,因为当 JPA 抛出 NoResultException 时,事务不会被标记为回滚。或者您可以使用 getResultList() 并检查大小是否正好为“1”,这样您就知道您已找到您的用户。
Additionally, I wouldn't return "[null]" if the user is not found, but throw a checked UserNotFoundException (to be defined). But this depends on the contract of the method you are going to implement.
此外,如果找不到用户,我不会返回“[null]”,而是抛出一个已检查的 UserNotFoundException(待定义)。但这取决于您将要实施的方法的契约。
回答by Mike
Michael said: "You can catch NoResultException in the catch-clause, because the transaction won't be marked as rollback, when JPA is throwing NoResultException.". It looks like for some jpa implementations, the NoResultException will rollbak transaction, which violates jpa specification, according to this article: NoResultException marks transaction rollback
Michael 说:“您可以在 catch 子句中捕获 NoResultException,因为当 JPA 抛出 NoResultException 时,事务不会被标记为回滚。”。看起来对于某些 jpa 实现,NoResultException 会回滚事务,这违反了 jpa 规范,根据这篇文章:NoResultException 标记事务回滚
回答by Frank
Catch NoResultException in try-catch block and handle it as per your requirement such as returning a null value.
在 try-catch 块中捕获 NoResultException 并根据您的要求处理它,例如返回空值。
Example of try-catch is detailed in the following link: http://www.javabrahman.com/j2ee/jpa/no-result-exception/
以下链接中详细介绍了 try-catch 示例:http: //www.javabrahman.com/j2ee/jpa/no-result-exception/
回答by koke24
If your application uses Spring Roo the most voted answer doesn't work: the exception is caught by the aspects and you never get the desired NoResultException
.
The only way to solve the issue is to use getResultList
and to check for zero, one or more results in the resulting List
, as stated in the second most voted answer.
如果您的应用程序使用 Spring Roo,则投票最多的答案不起作用:方面会捕获异常,而您永远不会得到所需的NoResultException
. 解决问题的唯一方法是使用getResultList
并检查结果中的零个、一个或多个结果List
,如投票数第二的答案所述。