Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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:

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:

List<Integer>

Any idea why there is a difference?

like image 744
Jochen Hebbrecht Avatar asked May 31 '12 08:05

Jochen Hebbrecht


1 Answers

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.

Change your code to Number,

List<Number> counts = (List<Number>) q.getResultList();
long count = counts.get(0).longValue();
like image 138
James Avatar answered Nov 12 '22 14:11

James