3

我正在尝试使用标准构建器从数据库中检索数据。它工作得很好,查询几乎是完美的......几乎。不幸的是,Java 不希望我使用 group by 或 distinct 作为查询的结果。如何让Java只检索唯一记录?我的代码在这里:

List<Documentation> documentationList = new ArrayList<>();
DatabaseConnector dc = new DatabaseConnector();
List<Predicate> criteria = new ArrayList<Predicate>();
EntityManager em = dc.getEntityManager();
CriteriaBuilder builder = em.getCriteriaBuilder();

CriteriaQuery<Documentation> select = builder.createQuery(Documentation.class);
Root<Documentation> u = select.from(Documentation.class);
Join<Documentation, DocumentationUser> du = u.join("documentationUserCollection",     JoinType.INNER);

javax.persistence.Query q = em.createQuery(select);
select.groupBy(u.<String>get("documentationId"));
select.distinct(true);

documentationList = q.setMaxResults(pageSize).setFirstResult(first).getResultList();
4

1 回答 1

2

尝试交换 2 行降低创建查询的行:

select.groupBy(u.<String>get("documentationId"));
select.distinct(true);

javax.persistence.Query q = em.createQuery(select);
于 2013-01-20T20:04:43.173 回答