1

DetachedCriteria这里有一个休眠代码。我在运行应用程序时收到一个java.util.ArrayList cannot be cast to java.lang.Long!基本上我在这里所做的是有两个列表,每个列表都有酒店和税收。这里的逻辑是如果指定了酒店和税,加载必要的数据!如果他们没有加载所有内容!当我未选择酒店并选择税收时,我收到上述错误。如果我选择酒店并将税款留空,我会得到预期的结果。

//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"));

请让我知道我在这里犯了什么错误!

4

1 回答 1

7

Restrictions.in("t.id",taxIds)在匹配集合中的项目时使用。

在这里Restrictions.eq("t.id",taxIds),taxIds 是 ArrayList,t.id 是 Long,所以java.util.ArrayList 不能强制转换为 java.lang.Long异常

于 2013-03-08T11:58:17.367 回答