【问题标题】:java.util.ArrayList cannot be cast to java.lang.Long hibernate detached criteriajava.util.ArrayList 不能强制转换为 java.lang.Long 休眠分离标准
【发布时间】:2013-03-08 11:50:14
【问题描述】:

我在这里有一个休眠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"));

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

【问题讨论】:

    标签: java spring hibernate struts2 detachedcriteria


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢老兄!那是确切的答案! :O 我想知道我是怎么错过这个简单的错误的! :O
    猜你喜欢
    • 2015-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-29
    • 1970-01-01
    • 2011-06-23
    • 1970-01-01
    • 2016-07-26
    相关资源
    最近更新 更多