如何在 JPA Critera API 中进行不同计数?

发布于 2024-11-13 07:47:09 字数 200 浏览 2 评论 0原文

我想这样做,但使用标准 API:

select count(distinct e) from Event e, IN(e.userAccessPermissions) p where p.principal = :principal and p.permission in (:permissions)

有什么想法吗?

I would like to do this but with the criteria API instead:

select count(distinct e) from Event e, IN(e.userAccessPermissions) p where p.principal = :principal and p.permission in (:permissions)

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

避讳 2024-11-20 07:47:09

您可以在 CriteriaBuilder 上使用 countDistinct

criteriaQuery.select(criteriaBuilder.countDistinct(entityRoot))

You can use countDistinct on CriteriaBuilder

criteriaQuery.select(criteriaBuilder.countDistinct(entityRoot))
っ〆星空下的拥抱 2024-11-20 07:47:09

在查询中使用 c.distinct(true) 。

有关更多示例,请参阅 http://relation.to/Bloggers/ATypesafeCriteriaQueryAPIForJPA

Use c.distinct(true) on your Query.

See http://relation.to/Bloggers/ATypesafeCriteriaQueryAPIForJPA for more samples.

三月梨花 2024-11-20 07:47:09

像这样?

Criteria crit = session.createCriteria(Event.class):
crit.createAlias("userAccessPermissions", "p");
crit.add(Restrictions.eq("p.principal", principal);
crit.add(Restrictions.in("p.permission", permissions);
crit.setProjection(Projections.countDistinct("id"));

Like this?

Criteria crit = session.createCriteria(Event.class):
crit.createAlias("userAccessPermissions", "p");
crit.add(Restrictions.eq("p.principal", principal);
crit.add(Restrictions.in("p.permission", permissions);
crit.setProjection(Projections.countDistinct("id"));
迷鸟归林 2024-11-20 07:47:09
    public long getCount(String xValue){
      EntityManager entityManager = this.getEntityManager();

      CriteriaBuilder cb = entityManager.getCriteriaBuilder();
      CriteriaQuery<Long> criteriaQuery = cb.createQuery(Long.class);
      Root<MyEntity> root = criteriaQuery.from(MyEntity.class);

      criteriaQuery.select(cb.count(criteriaQuery.from(MyEntity.class)));

      List<Predicate> predicates = new ArrayList<>();

      Predicate xEquals = cb.equal(root.get("x"), xValue);
      predicates.add(xEquals);

      criteriaQuery.select(cb.countDistinct(root));
      criteriaQuery.where(predicates.toArray(new Predicate[0]));

      return entityManager.createQuery(criteriaQuery).getSingleResult();


    }

有了Spring Data Jpa,我们可以使用这个方法:

     /*
     * (non-Javadoc)
     * @see org.springframework.data.jpa.repository.JpaSpecificationExecutor#count(org.springframework.data.jpa.domain.Specification)
     */
    @Override
    public long count(@Nullable Specification<T> spec) {
        return executeCountQuery(getCountQuery(spec, getDomainClass()));
    }
    public long getCount(String xValue){
      EntityManager entityManager = this.getEntityManager();

      CriteriaBuilder cb = entityManager.getCriteriaBuilder();
      CriteriaQuery<Long> criteriaQuery = cb.createQuery(Long.class);
      Root<MyEntity> root = criteriaQuery.from(MyEntity.class);

      criteriaQuery.select(cb.count(criteriaQuery.from(MyEntity.class)));

      List<Predicate> predicates = new ArrayList<>();

      Predicate xEquals = cb.equal(root.get("x"), xValue);
      predicates.add(xEquals);

      criteriaQuery.select(cb.countDistinct(root));
      criteriaQuery.where(predicates.toArray(new Predicate[0]));

      return entityManager.createQuery(criteriaQuery).getSingleResult();


    }

With Spring Data Jpa, we can use this method:

     /*
     * (non-Javadoc)
     * @see org.springframework.data.jpa.repository.JpaSpecificationExecutor#count(org.springframework.data.jpa.domain.Specification)
     */
    @Override
    public long count(@Nullable Specification<T> spec) {
        return executeCountQuery(getCountQuery(spec, getDomainClass()));
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文