Java JPA EntityManager.getSingleResult() 为 COUNT 查询返回什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3574029/
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
What does JPA EntityManager.getSingleResult() return for a COUNT query?
提问by Simon Gibbs
What does EntityManager.getSingleResult()
return for a COUNT query?
EntityManager.getSingleResult()
COUNT 查询返回什么?
So.. what is the precise runtime type of foo?
那么.. foo 的精确运行时类型是什么?
Object foo = em.createQuery("SELECT COUNT(t) FROM com.company.Thing t WHERE prop = :param")
.setParameter("param", value).getSingleResult();
采纳答案by Simon Gibbs
COUNT(t) specifically returns java.lang.Long. When its appears on its own in this context it is returned as-is.
COUNT(t) 专门返回 java.lang.Long。当它在此上下文中单独出现时,它会按原样返回。
(In other contexts the Long generated by COUNT may be wrapped, but not today.)
(在其他情况下,由 COUNT 生成的 Long 可能会被包装,但今天不会。)
回答by Petar Minchev
Obviously, a number which is the count from the query:) Its type by specification is Long.
显然,一个数字是查询中的计数:) 它的类型按规范是 Long。
回答by Bogdan
AFAIK COUNT function returns Long
AFAIK COUNT 函数返回 Long
回答by Pascal Thivent
As per the JPA specification, COUNT returns a Long:
根据JPA 规范, COUNT 返回一个 Long:
4.8.4 Aggregate Functions in the SELECT Clause The result of a query
may be the result of an aggregate function applied to a path expression.
The following aggregate functions can be used in the SELECT clause of a query: AVG, COUNT, MAX, MIN, SUM.
For all aggregate functions except COUNT, the path expression that is the argument to the aggregate function must terminate in a state-field. The path expression argument to COUNT may terminate in either a state-field or a association-field, or the argument to COUNT may be an identification variable.
Arguments to the functions SUM and AVG must be numeric. Arguments to the functions MAX and MIN must correspond to orderable state-field types (i.e., numeric types, string types, character types, or date types).
The Java type that is contained in the result of a query using an aggregate function is as follows:
- COUNT returns Long.
- MAX, MIN return the type of the state-field to which they are applied.
- AVG returns Double.
- SUM returns Long when applied to state-fields of integral types (other than BigInteger); Double when applied to state-fields of floating point types; BigInteger when applied to state-fields of type BigInteger; and BigDecimal when applied to state-fields of type BigDecimal.
If SUM, AVG, MAX, or MIN is used, and there are no values to which the aggregate function can be applied, the result of the aggregate function is NULL.
If COUNT is used, and there are no values to which COUNT can be applied, the result of the aggregate function is 0.
The argument to an aggregate function may be preceded by the keyword DISTINCT to specify that duplicate values are to be eliminated before the aggregate function is applied.
Null values are eliminated before the aggregate function is applied, regardless of whether the keyword DISTINCT is specified.
4.8.4 SELECT 子句中的聚合函数查询的结果
可能是应用于路径表达式的聚合函数的结果。
可以在查询的 SELECT 子句中使用以下聚合函数:AVG、COUNT、MAX、MIN、SUM。
对于除 COUNT 之外的所有聚合函数,作为聚合函数参数的路径表达式必须在状态字段中终止。COUNT 的路径表达式参数可以在状态字段或关联字段中终止,或者 COUNT 的参数可以是标识变量。
函数 SUM 和 AVG 的参数必须是数字。函数 MAX 和 MIN 的参数必须对应于可排序的状态字段类型(即数字类型、字符串类型、字符类型或日期类型)。
使用聚合函数的查询结果中包含的 Java 类型如下:
- COUNT 返回 Long。
- MAX, MIN 返回应用它们的状态字段的类型。
- AVG 返回双精度值。
- SUM 在应用于整数类型(BigInteger 除外)的状态字段时返回 Long;应用于浮点类型的状态字段时为双倍;BigInteger 当应用于 BigInteger 类型的状态字段时;和 BigDecimal 当应用于 BigDecimal 类型的状态字段时。
如果使用 SUM、AVG、MAX 或 MIN,并且没有可以应用聚合函数的值,则聚合函数的结果为 NULL。
如果使用 COUNT,并且没有可以应用 COUNT 的值,则聚合函数的结果为 0。
聚合函数的参数前面可以有关键字 DISTINCT 以指定在应用聚合函数之前要消除重复值。
无论是否指定了关键字 DISTINCT,在应用聚合函数之前都会消除空值。
回答by lgu
NB : there's a difference between JQPLand Nativequery
注意:JQPL和Native查询是有区别的
Query query = em.createQuery("SELECT COUNT(p) FROM PersonEntity p " );
query.getSingleResult().getClass().getCanonicalName()
--> java.lang.Long
query.getSingleResult().getClass().getCanonicalName()
--> java.lang.Long
Query query = em.createNativeQuery("SELECT COUNT(*) FROM PERSON " );
query.getSingleResult().getClass().getCanonicalName()
--> java.math.BigInteger
query.getSingleResult().getClass().getCanonicalName()
--> java.math.BigInteger
回答by user6645244
Native queries can return different objects for a COUNT based on the database driver; however, all those objects extend java.lang.Number, which implements the longValue() method.
本机查询可以根据数据库驱动程序为 COUNT 返回不同的对象;然而,所有这些对象都扩展了 java.lang.Number,它实现了 longValue() 方法。
回答by Tiago Moura
You can return directly integer, rather than a long.
您可以直接返回整数,而不是长整数。
return query.getSingleResult() != null ? Integer.parseInt(query.getSingleResult().toString()) : 0;