4

首先我的设置:

  1. mysql-connector-java 5.1.24
  2. 休眠核心 4.1.10.Final

运行此条件查询时出现 ClassCastException :

Criteria sellableItemsCriteria = session.createCriteria(MarketData.class, "md");
sellableItemsCriteria.add(Restrictions.in("region", regions));
sellableItemsCriteria.add(Restrictions.in("itemTypeId", items));
DetachedCriteria sellOrderSizeCriteria = DetachedCriteria.forClass(MarketOrder.class);
sellOrderSizeCriteria.add(Restrictions.eq("marketDataId", "md.id"));
sellOrderSizeCriteria.add(Restrictions.eq("bid", false));
sellOrderSizeCriteria.setProjection(Projections.count("marketDataId"));
sellableItemsCriteria.add(Subqueries.lt(0L, sellOrderSizeCriteria));

例外:

Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Long

问题出在这一行(首先我尝试使用 0 而不是 0L 并且我得到 Integer cannot be cast to Long 所以我切换到 Long):

sellableItemsCriteria.add(Subqueries.lt(0L, sellOrderSizeCriteria));

这是我要运行的 mysql 查询:

SELECT md.* FROM `marketdata` md
WHERE md.region IN (:regions) 
AND md.item_typeID IN (:items) 
AND (SELECT COUNT(marketData_id) FROM `marketorder` WHERE marketData_id = md.id AND bid = 0) > 0

如何解决演员阵容问题?

或者也许有更好的方法可以用 Criteria 做到这一点?

谢谢

4

1 回答 1

13

我很确定您会发现异常实际上来自这一行:

sellOrderSizeCriteria.add(Restrictions.eq("marketDataId", "md.id"));

此行尝试将 的marketDataId属性MarketOrder与 String进行比较"md.id"。那不是你想做的。您要做的是将 的marketDataId属性与MarketOrderid属性进行比较md。因此,您需要使用eqProperty()代替:

sellOrderSizeCriteria.add(Restrictions.eqProperty("marketDataId", "md.id"));
于 2013-04-28T06:24:59.270 回答