我这里有一个 Hibernate 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"));
请让我知道我在这里犯了什么错误!
最佳答案
在匹配集合中的项目时使用 Restrictions.in("t.id",taxIds)
。
这里Restrictions.eq("t.id",taxIds)
,taxIds是ArrayList,t.id是Long,所以java.util.ArrayList不能强制转换java.lang.Long异常
关于java.util.ArrayList 无法转换为 java.lang.Long hibernate 分离条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15293344/