2013-03-08 48 views
1

我有一個Hibernate DetachedCriteria代碼在這裏。我在運行應用程序時收到java.util.ArrayList cannot be cast to java.lang.Long!基本上我在這裏做的是有兩個列表,每個都有酒店和稅。這裏的邏輯是如果指定了酒店和稅收,則加載必要的數據!如果他們不加載任何東西!當我保留酒店未選定和稅收選擇我收到上述錯誤。如果我選擇酒店並將稅務清空,我會按預期得到結果。java.util.ArrayList不能轉換爲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")); 

請讓我知道我在這裏做什麼錯誤!

回答

6

使用Restrictions.in("t.id",taxIds)同時匹配集合中的項目。

這裏Restrictions.eq("t.id",taxIds),taxIds是ArrayList和t.id長,所以自帶java.util.ArrayList中不能被強制轉換爲java.lang.Long中例外

+0

感謝隊友!那就是確切的答案! :O我想知道我是如何錯過這個簡單的錯誤! :o – 2013-03-08 12:00:06