0

I have the below code

StoredProcedureQuery storedProcedure = em.createStoredProcedureQuery("pkg_p.proc_p");
         storedProcedure.getResultList();

stored proc returns a ref cursor equivalent to the below query

 select 1 as id_col, 'My Name ' as name , 1000 as sal from dual;

I have a pojo class MyTable which is equivalent to the result set return type of the query

 public class MyTable {
 private Long idCol;
 private String name;
 private Long sal;

 /// setter and getters omitted for brevity

}

for(Object[]row: resultSet)
        {
MyTable mt = new MyTable ();
mt.setIdCol((Long)row[0]);  ///throw class cast exception
}

How to resolve the below error

Request processing failed; nested exception is java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.Long

Even though row[0] has Long values like 1,2,10,687 etc

2
  • You can change the Data-Type Long to Number for both idCol and sal or you can simply cast the value to Long by invoking longValue() method on the object. Commented Oct 26, 2018 at 11:13
  • what is the the problem? idCol has integer value in DB .why spring is treating as Big Decimal? Commented Oct 26, 2018 at 11:25

1 Answer 1

2

You can't cast Big Decimal to Long how ever you can get the Long value from it using

row[0].longValue()

if that doesn't work cast row to big decimal and

(BigDecimal) row[0].longValue()

so your code would look something like this

mt.setIdCol((BigDecimal)row[0].longValue());

or try to change the loop to

for(BigDecimal[] row : resultSet)

if you don't want to use casting

after reading your comments.

    for(Object[] row: resultSet) {
        MyTable mt = new MyTable();
        if(resultSet instanceof BigDecimal) {
            mt.setIdCol((BigDecimal)row[0].longValue());
        } else if (resultSet instanceof String){
            ....
        }
    }
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks it seems sping is by default treating any numeric value as Big Decimal.Right?
i am not able to row[0].longValue() .Eclipse doesnt give option
Do you have the correct case? try mt.setIdCol(row[0].longValue()); if that doesn't work cast it to BigDecimal like this Do you have the correct case? try mt.setIdCol((BigDecimal )row[0].longValue()); ``
Edited answer let me know if you are still having problems
I have provided a variety of solutions for you to try and see what you like eclipse wasn't giving you the option because the loop item was an Object.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.