javax.persistence.Query.getResultList() 可以返回 null 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1115480/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:56:37  来源:igfitidea点击:

Can javax.persistence.Query.getResultList() return null?

javahibernatejpa

提问by rdk

And if so, under what circumstances?

如果是这样,在什么情况下?

Javadoc and JPA spec says nothing.

Javadoc 和 JPA 规范什么也没说。

采纳答案by Arthur Ronald

You are right. JPA specification says nothing about it. But Java Persistence with Hibernate book, 2nd edition, says:

你是对的。JPA 规范对此一无所知。但是Java Persistence with Hibernate book, 2nd edition说:

If the query result is empty, a null is returned

如果查询结果为空,则返回空值

Hibernate JPA implementation (Entity Manager) return null when you call query.getResultList() with no result.

当您调用 query.getResultList() 没有结果时,Hibernate JPA 实现(实体管理器)返回 null。

UPDATE

更新

As pointed out by some users, it seems that a newest version of Hibernate returns an empty list instead.

正如一些用户所指出的,似乎最新版本的 Hibernate 返回一个空列表。

An empty list is returned in Eclipselink as well when no results are found.

当未找到任何结果时,Eclipselink 中也会返回一个空列表。

回答by djna

If the specs said it could't happen, would you belive them? Given that your code could conceivably run against may different JPA implementations, would you trust every implementer to get it right?

如果规范说这不可能发生,你会相信他们吗?考虑到您的代码可能会运行在可能不同的 JPA 实现上,您会相信每个实现者都能做到吗?

No matter what, I would code defensively and check for null.

无论如何,我都会防御性地编码并检查是否为空。

Now the big question: should we treat "null" and an empty List as synonymous? This is where the specs should help us, and don't.

现在最大的问题是:我们应该将“null”和空列表视为同义词吗?这是规范应该帮助我们的地方,而不是。

My guess is that a null return (if indeed it could happen) would be equivalent to "I didn't understand the query" and empty list would be "yes, understood the query, but there were no records".

我的猜测是 null 返回(如果确实可能发生)将等同于“我不理解查询”,而空列表将是“是的,理解查询,但没有记录”。

You perhaps have a code path (likely an exception) that deals with unparsable queries, I would tend to direct a null return down that path.

您可能有一个处理不可解析查询的代码路径(可能是一个例外),我倾向于在该路径下直接返回空值。

回答by Andrew Simons

Contrary to Arthur's post, when I actually ran a query which no entities matched I got an empty list, not null. This is using Hibernate and is what I consider correct behaviour: an empty list is the correct answer when you ask for a collection of entities and there aren't any.

与 Arthur 的帖子相反,当我实际运行一个没有实体匹配的查询时,我得到了一个空列表,而不是 null。这是使用 Hibernate 并且我认为是正确的行为:当您要求实体集合而没有任何实体时,空列表是正确的答案。

回答by Al Scherer

Of course, if you test the result set with Jakarta's CollectionUtils.isNotEmpty, you're covered either way.

当然,如果您使用 Jakarta 的 CollectionUtils.isNotEmpty 测试结果集,则无论哪种方式都适用。

回答by Charles Follet

If you take a close look at the org.hibernate.loader.Loader(4.1) you will see that the list is always initialized inside the processResultSet() method (doc, source).

如果您仔细查看org.hibernate.loader.Loader(4.1),您将看到该列表始终在 processResultSet() 方法(docsource)内初始化。

protected List processResultSet(...) throws SQLException {
   final List results = new ArrayList();

   handleEmptyCollections( queryParameters.getCollectionKeys(), rs, session );
   ...
   return results;

}

So I don't think it will return null now.

所以我认为它现在不会返回 null。

回答by Cyril Sojan

Query.getResultList()returns an empty list instead of null. So check isEmpty()in the returned result, and continue with the rest of the logic if it is false.

Query.getResultList()返回一个空列表而不是null. 所以检查isEmpty()返回的结果,如果它是假的,则继续执行其余的逻辑。

回答by wile the coyote

Given the implementation of getResultsList()in org.hibernate.ejb.QueryImplclass, it is possible to return a null:

鉴于getResultsList()in org.hibernate.ejb.QueryImplclass的实现,有可能返回一个null

public List getResultList() {
    try {
        return query.list();
    }
    catch (QueryExecutionRequestException he) {
        throw new IllegalStateException(he);
    }
    catch( TypeMismatchException e ) {
        throw new IllegalArgumentException(e);
    }
    catch (HibernateException he) {
        em.throwPersistenceException( he );
        return null;
    }

My hibernate version is: 3.3.1.GA

我的休眠版本是:3.3.1.GA