Home > other >  How to use returned Object of .getSingleResult() EntityManager
How to use returned Object of .getSingleResult() EntityManager

Time:07-19

I have the following SQL Statement and I used .getSingleResult() to get a Object from the EntityManager: SELECT sum(pos_netto) as revenue from Auftraege_Positionen_BI_Test where art_nr = :artNr I stored it in the Object returnObject. Now my Question is how can I get the value I selected via the SQL Statement out of the Object?

CodePudding user response:

Given your case It seems like you want to take out the sum which most probably is a double value You can simply use

double returningSum=(Double) query.getSingleResult();

Or to check if it is null and taking appropriate actions like assigning 0

Object result=(Object) query.getSingleResult();
double returningSum=result==null?0.0: (Double) result;

CodePudding user response:

Based on this link , sum returns Long or Double or BigInteger or BigDecimal etc value, so I think you don't need to do any conversion when using query.getSingleResult().

Just check what is type of pos_netto field in your class & use that type directly as a return type of query.getSingleResult().

  • Related