1

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!

1 Answer 1

7

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

Sign up to request clarification or add additional context in comments.

1 Comment

thanks mate! that was the exact answer! :O I wonder how I missed this simple error! :O

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.