Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JPA getResultList()为MySQL返回BigInteger,但为Microsoft SQL server返回Integer_Java_Mysql_Sql Server_Jpa - Fatal编程技术网

Java JPA getResultList()为MySQL返回BigInteger,但为Microsoft SQL server返回Integer

Java JPA getResultList()为MySQL返回BigInteger,但为Microsoft SQL server返回Integer,java,mysql,sql-server,jpa,Java,Mysql,Sql Server,Jpa,我有以下方法: 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); 当我想要获得计数列表(将是2行)时,我执行以下操作:

我有以下方法:

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);
当我想要获得计数列表(将是2行)时,我执行以下操作:

List<BigInteger> counts = (List<BigInteger>) q.getResultList();
List counts=(List)q.getResultList();
这在MySQL中运行良好。但只要我连接到MS SQL server,就会得到一个整数对象列表:

List<Integer>
列表

知道为什么会有差异吗?

它是在服务器端定义的

“返回SELECT语句检索到的行中expr的非空值的计数。结果为BIGINT值。”

“返回类型


int“

JPA定义了JPQL查询的返回类型,但是对于原生SQL查询,您可以得到数据库返回的任何内容。这就是本机SQL查询的要点

将代码更改为数字

List<Number> counts = (List<Number>) q.getResultList();
long count = counts.get(0).longValue();
List counts=(List)q.getResultList();
long count=counts.get(0.longValue();

您的标题是向后的。您使用的是哪个JPA实现?您是否尝试过明确地设置预期的返回类型,如
getEntityManager().createNativeQuery(“从表1中选择计数(1),其中列=:column_id“+”UNION“+”从表2中选择计数(1),其中列=:column_id”,Integer.class)哈桑:谢谢,我已经更新了标题:谢谢,我已经测试过了,但是我得到了这个异常:org.hibernate.MappingException:Unknown entity:java.lang.IntegerHmmm。。。但JPA不就是要把数据库层和处理层分开吗?我怎样才能得到统一的结果?我只想使用biginger或Integer。我不管是哪一个,只有1:-)正如AhamedMusatafaM所说,您是否尝试过查询q=getEntityManager().createNativeQuery(“从表1中选择计数(1),其中列=:列id“+”联合“+”从表2中选择计数(1),其中列=:列id”,Integer.class);tagtraeumer:如果我试着这么做,我会得到:javax.persistence.PersistenceException:org.hibernate.MappingException:Unknown entity:java.lang.IntegerThanks James,你的解决方案就是补丁!:-)