javaspringhibernatestruts2detachedcriteria

java.util.ArrayList cannot be cast to java.lang.Long hibernate detached criteria


I have a Hibernate DetachedCriteria Code here. I receive a java.util.ArrayList cannot be cast to java.lang.Long when I run the application! basically what I do here is there are two lists each with hotels and tax. the logic here is if the hotels and tax is specified, load the necessary data! if they are not load everything! when I keep hotels unselected and tax selected I receive the above error. If I select hotels and leave tax empty I get the result as expected.

//Use if hotel is specified
    if(searchCriteria.getHotel().getId() != null){
        dc.add(Restrictions.eq("h.id", searchCriteria.getHotel().getId()));
    }else if(hotels != null && !hotels.isEmpty()){
        Collection<Long> hotelIds = new ArrayList<Long>();
        for(Hotel h : hotels){
            hotelIds.add(h.getId());
        }
        dc.add(Restrictions.eq("h.id",hotelIds));
    }

    //use if tax is specified
    if(searchCriteria.getTax().getId() != null){
        dc.add(Restrictions.eq("t.id", searchCriteria.getTax().getId()));
    }else if(tax != null && !tax.isEmpty()){
        Collection<Long> taxIds = new ArrayList<Long>();
        for(Tax t : tax){
            taxIds.add(t.getId());
        }
        dc.add(Restrictions.eq("t.id",taxIds));
    }

    //Order Result
    dc.addOrder(Order.asc("h.id"));
    dc.addOrder(Order.asc("t.code"));

please let me know what mistake I am doing here!


Solution

  • Use Restrictions.in("t.id",taxIds) while matching an item from a collection.

    Here Restrictions.eq("t.id",taxIds), taxIds is ArrayList and t.id is Long, so comes java.util.ArrayList cannot be cast to java.lang.Long exception