所以我有以下代码
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); 我错了吗?
感谢您的帮助