Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA SUM returns long

I am using JPA and I have the following query

@Query(value = "SELECT  NEW com.example.model.company (
 SUM (CASE WHEN c.company_description_id = 3 THEN  CAST (c.value AS int) ELSE 0 END )
 FROM Company c 
 GROUP BY c.company_id ")

c.value is stored as INT in the database, but JPA returns it as long which is wrong. Any ideas what I am doing wrong here. I just want to return the exact value that is stored in the database. It is a simplified version of my query.

like image 206
Waleed Asif Avatar asked Sep 14 '25 11:09

Waleed Asif


1 Answers

According to the JPA spec 4.5.8

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.

like image 173
Christian Beikov Avatar answered Sep 17 '25 01:09

Christian Beikov