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

13,524

Solution 1

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

Now it's working fine.

Solution 2

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

Solution 3

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.

Share:
13,524

Related videos on Youtube

Karthik
Author by

Karthik

Nerd...Next...Door

Updated on June 04, 2022

Comments

  • Karthik
    Karthik almost 2 years

    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

    • Lino
      Lino over 4 years
      is o.OrderId a BigDecimal by chance?
    • Karthik
      Karthik over 4 years
      No. It is of type Long
    • Rozart
      Rozart over 4 years
      What kind of database are you using? If Oracle then maybe that is the cause of your issue: stackoverflow.com/a/5380867/1506009
    • Karthik
      Karthik over 4 years
      Yeah. I'm Using Oracle. How do I convert it to Long though ?
    • Abra
      Abra over 4 years
      What is the [Oracle] data-type of column OrderId in database table Order ? Is it NUMBER ?
    • Karthik
      Karthik over 4 years
      Yes, data-type of column OrderId in database table is NUMBER
    • Abra
      Abra over 4 years
      By default, Oracle JDBC driver maps Oracle database NUMBER data-type to java BigDecimal class.
  • Karthik
    Karthik over 4 years
    added code for orderDAO.getOrderIdByUserId(userId)
  • Michael Piefel
    Michael Piefel over 4 years
    It may be working fine, but it’s wrong internally. Fine thing you give yourself the accepted answer when you do not comprehend what’s really going on behind the scenes. In fact, I do not even believe that what you say here is right, because your alleged replacement will not compile – at least not with the Hibernate that I have access to.