java JPA getResultList() 为 MySQL 返回 BigInteger 但为 Microsoft SQL server 返回 Integer
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10830323/
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
JPA getResultList() returns BigInteger for MySQL but Integer for Microsoft SQL server
提问by Jochen Hebbrecht
I have following method:
我有以下方法:
Query q = getEntityManager().createNativeQuery("SELECT COUNT(1) FROM table1 WHERE column = :column_id " + "UNION " + "SELECT COUNT(1) FROM table2 WHERE column = :column_id");
q.setParameter("column_id", column_id);
When I want to get the list of counts (which will be 2 rows), I perform this action:
当我想获取计数列表(将是 2 行)时,我执行以下操作:
List<BigInteger> counts = (List<BigInteger>) q.getResultList();
This is working fine in MySQL. But as soon as I connect to MS SQL server, I'm getting a List of Integer objects:
这在 MySQL 中运行良好。但是一旦我连接到 MS SQL 服务器,我就会得到一个整数对象列表:
List<Integer>
Any idea why there is a difference?
知道为什么会有区别吗?
回答by James
JPA defines the return types for JPQL queries, but for native SQL queries you get whatever the database returns. That is kind of the point with native SQL queries.
JPA 定义了 JPQL 查询的返回类型,但对于本机 SQL 查询,您将获得数据库返回的任何内容。这就是本机 SQL 查询的重点。
Change your code to Number,
将您的代码更改为数字,
List<Number> counts = (List<Number>) q.getResultList();
long count = counts.get(0).longValue();
回答by tagtraeumer
it is defined serverside
它被定义为服务器端
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_count
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_count
"Returns a count of the number of non-NULL values of expr in the rows retrieved by a SELECT statement. The result is a BIGINT value. "
“返回由 SELECT 语句检索的行中 expr 的非 NULL 值数量的计数。结果是一个 BIGINT 值。“
http://msdn.microsoft.com/en-us/library/aa258232%28v=sql.80%29
http://msdn.microsoft.com/en-us/library/aa258232%28v=sql.80%29
"Return Types
"返回类型
int"
整数”