JPA getResultList () returns BigInteger for MySQL, but Integer for Microsoft SQL Server

I have the 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 a list of counters (which will be 2 lines), I perform this action:

 List<BigInteger> counts = (List<BigInteger>) q.getResultList(); 

This works fine in MySQL. But as soon as I connect to the MS SQL server, I get Integer objects with an integer:

 List<Integer> 

Any idea why there is a difference?

+7
source share
2 answers

JPA defines return types for JPQL queries, but for your own SQL queries you get what the database returns. This is a kind of point with your own SQL queries.

Change your code to Number,

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

determined by serveride

http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_count

"Returns the count of the number of non-NULL expr values ​​in the rows received by the SELECT statement. The result is a BIGINT value."

http://msdn.microsoft.com/en-us/library/aa258232%28v=sql.80%29

"Return types

INT "

0
source

All Articles