java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.Long

I have this block of code in OrderService.java

    public void deleteOrderByUserId(int userId){
       List<Long> orderIds = orderDAO.getOrderIdByUserId(userId);
       int deleteOrders = orderDAO.deleteOrders(orderIds);
    }

This is the code in orderDAO.java

public List getOrderIdByUserId(int userId) {
    StringBuilder queryStr = new StringBuilder("select distinct u.OrderId from ");
    queryStr.append("User u where ");
    queryStr.append("u.UserId in (:key)");

    return getHibernateTemplate().getSessionFactory()
            .getCurrentSession().createSQLQuery(queryStr.toString())
            .setParameter("key", userId).list();
}


    public int deleteOrders(List<Long> orderIds){
      final String deleteOrder = "delete from Order o where o.OrderId in (:orderIds)";
      final Query hibernateQuery = getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(deleteOrder);
      hibernateQuery.setParameterList("orderIds", orderIds);
      int count = hibernateQuery.executeUpdate();   
      return count;
     }    

I'm getting an java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.Long Exception while executing this step int count = hibernateQuery.executeUpdate();

What's wrong with that code and how to get rid of that exception


To get the long value of a BigDecimal object you can call the .longValue() method on it.

java.math.BigInteger cannot be cast to java.lang.Long, Your error might be in this line: List<Long> result = query.list();. where query.list() is returning a BigInteger List instead of Long list. Try to  The exception: java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.Integer. Also happens if I use Long instead of Integer: java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.Long. The Exception happens when accessing the results, not during the query execution.


orderDAO.getOrderIdByUserId(userId) returns a list of BigDecimal, not of Long, I would guess. It’s hard to tell without the code for that method.


EDIT (now that the code is there): Considering https://stackoverflow.com/a/5380867/1506009, you can see that some databases (Oracle comes to mind) return BigDecimal (or rather List<BigDecimal>) when calling list() in Hibernate. Your Java code is faulty in using a raw List and just assuming some type when it is indeed another.

getOrderIdByUserId() could return List<? extends Number>, which would match both Long and BigDecimal; or it could return List<BigDecimal> if that’s the truth. To not use raw types!

setParameterList() allows a third parameter, the type of the list elements. Use that.

java.math.BigInteger cannot be cast to java.lang.Integer, You can use: Integer grandChildCount = ((BigInteger) result[1]).intValue();. Or perhaps cast to Number to cover both Integer and BigInteger  Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more how to solve java.lang.ClassCastException: java.math.BigDecimal cannot be cast to [Ljava.lang.Object; in java


Instead of using hibernateQuery.setParameterList("orderIds", orderIds);, I've updated it to hibernateQuery.setBigDecimal("orderIds", orderIds);

Now it's working fine.

Fix for java.math.BigInteger cannot be cast to java.lang.Integer?, yourVariableName=((BigInteger) yourBigIntegerValue).intValue();. Here is the Java code to convert java.math.BigInteger cast to java.lang. java.lang.Long and java.math.BigInteger are in a different hierarchies. They cannot be castes to each other. They cannot be castes to each other. You can create new BigInteger by using static factory method :


java.math.BigDecimal cannot be cast to java.lang.Double, ClassCastException "java.math.BigDecimal cannot be cast to java.lang.Double". Apex. Last updated 2014-09-22 ·Reference W-2157780 ·Reported By 13 users. I'm getting the following exception. Caused by: java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Integer with the following code List queryResult = query.lis


Caused by: java.lang.ClassCastException: java.math.BigDecimal , Caused by: java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.Long, Programmer Sought, the best programmer technical posts  Summary When an insert is done on a lookup field from custom object to User object, the insert fails with ClassCastException "java.math.BigDecimal cannot be cast to java.lang.Double", when there is a filter on a User field, and the filter type is "required."


[#IBATIS-644] ClassCastException: BigDecimal cannot be cast to , The exception: java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.Integer Also happens if I use Long instead of  As the exception is telling you, you cannot cast a Long to a BigInteger or vice versa. I was discussing casting here yesterday. So, you are getting a BIGINT back, and it is within the range of a Long?