所以我有以下代码
DetachedCriteria subquery = DetachedCriteria.forClass(Component.class);
subquery.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
subquery.createAlias("files", "files");
subquery.createAlias("files.review", "review");
subquery.createAlias("review.observers", "observer", Criteria.LEFT_JOIN);
subquery.add(Restrictions.or(Restrictions.eq("review.owner", user), Restrictions.eq("observer.id", user.getId())));
subquery.setProjection(Projections.groupProperty("review.id"));
DetachedCriteria subquery2 = DetachedCriteria.forClass(ReviewNotification.class);
subquery2.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
subquery2.add(Subqueries.propertyIn("review", subquery));
subquery2.add(Restrictions.eq("reviewer", user));
subquery2.setProjection(Projections.groupProperty("review"));
Criteria criteria = session.createCriteria(Component.class);
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
criteria.createAlias("files", "files");
criteria.createAlias("files.review", "review");
criteria.add(Subqueries.propertyIn("review.id", subquery2));
现在我的问题是,如果我执行以下操作,我会得到2作为返回值(这是正确的)
List<Component> list = (List<Component>) criteria.list();
for (Component reviewNotification : list) {
System.out.println(reviewNotification);
}
return new Long(list.size());
但如果我这样做
criteria.setProjection(Projections.rowCount());
return (Long) criteria.uniqueResult();
我得到了4。
任何人都知道为什么行计数失败?
我刚试过别的东西
criteria.setProjection(Projections.countDistinct("id"));
这也有效。那么有谁知道发生了什么?我想这个问题来自criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);我错了吗?
感谢您的帮助
答案 0 :(得分:1)
我似乎方法setProjection覆盖
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
这可以解释为什么返回数据不是唯一的