Waleed Asif
Waleed Asif

Reputation: 199

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.

Upvotes: 4

Views: 5148

Answers (1)

Christian Beikov
Christian Beikov

Reputation: 16400

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.

Upvotes: 3

Related Questions