简体   繁体   English

映射到pojo列表的JPA命名查询的结果在从查询执行中未收到任何数据时不返回空列表

[英]Result of JPA named query mapped to a list of pojo doesn't return an empty list when no data is received from query execution

I have a named query in jpa ( hibernate implementation ), the result of which is to be mapped to a list of custom pojo( myresult.java ). 我在jpahibernate实现 )中有一个命名查询,其结果将映射到自定义pojo( myresult.java )列表。 In one case the query doesn't return any values and I was expecting an empty list to be returned. 在一种情况下,查询不返回任何值,我希望返回一个空列表。 Instead I am getting a list with one object of type " myresult ", with all the members initialized as null. 相反,我得到一个列表,其中包含一个类型为“ myresult ”的对象,所有成员都初始化为null。 I don't understand why this is happening...any help is much appreciated.. Thanks in advance. 我不明白为什么会这样......任何帮助都非常感谢..提前致谢。

I think the reason of your problem is explained in this link 我认为你的问题的原因在这个链接中解释

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

In JPA in place to return empty, returns a null, then in your case add null to the list to return, I worked with spring-data and return a empty list, i dont know why hibernate decide to use this approach in place of return an empty list 在JPA中返回空,返回null,然后在你的情况下将null添加到列表中返回,我使用spring-data并返回一个空列表,我不知道为什么hibernate决定使用这种方法代替返回一个空列表

After quite a bit of trials I found that Jpa hibernate implementation has it that if only aggregated values are to be returned from a named query then in "no data" case, an empty list of pojos is not returned. 经过相当多的试验后,我发现Jpa hibernate实现了如果只从命名查询返回聚合值,那么在“无数据”的情况下,不返回空列表的pojos。 Instead a list with a single object which has appropriate defaults set for attributes (null for sum, 0 for count etc) is returned. 而是返回具有单个对象的列表,该对象具有为属性设置的适当默认值(对于sum为null,对于count等为0)。 However, if only column values or column values along with aggregates are selected, then an empty list will be returned. 但是,如果仅选择列值或列值以及聚合,则将返回空列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM