简体   繁体   English

使用Criteria Builder运行带有IN子句的查询

[英]Run Query with IN clause using Criteria Builder

I need some help to run the query bellow, using the CriteriaBuilder aproach: 我需要一些帮助,以使用CriteriaBuilder方法运行以下查询:

select count(*) from TABLE_VIEW_SEARCH where
        person_ID in (select person_ID from TABLE_PERSON
        where region_ID = 1001) and postal_cd ='AL';

I try this but it failed: 我尝试了一下,但是失败了:

    CriteriaBuilder cb = ..;

    CriteriaQuery<Long> criteriaQuery= cb.createQuery(Long.class);

    Root<ViewSearchEntity> from= criteriaQuery.from(ViewSearchEntity.class);

    Subquery<Long> subQuery = criteriaQuery.subquery(Long.class);
    Root<PersonEntity> subFrom = subQuery .from(PersonEntity.class);

    subQuery .where(cb.equal(subFrom.get(PersonEntity_.region_ID ), region_ID ));

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

    criteriaQuery.where(meFrom.in(subQuery );
    TypedQuery<Long> query = getEntityManager().createQuery(criteriaQuery);
    Long result = query.getSingleResult();

I reach a solution, basicaly I managed to do the join condition: 我找到了一个解决方案,基本上我设法做到了加入条件:

    CriteriaBuilder cb = ..;

    CriteriaQuery<Long> criteriaQuery= cb.createQuery(Long.class);

    Root<ViewSearchEntity> from= criteriaQuery.from(ViewSearchEntity.class);

    Predicate predicate1 = cb.equal(from.get(ViewSearchEntity.postal_cd),"AL");

    Subquery<Long> subQuery = criteriaQuery.subquery(Long.class);

    Root<PersonEntity> subRoot = subQuery.from(PersonEntity.class);

    Join<PersonEntity, RegionEntity_> region= subRoot.join(RegionEntity_.region);

    subQuery.select(region.get(RegionEntity_.regionId)).where(
            cb.equal(subRoot.get(PersonEntity_.region), regionId));

    Predicate predicateInClause = root.get(ViewSearchEntity_.postal_cd).in(subQuery);

    return getEntityManager().createQuery(criteriaQuery.select(cb.count(root)).where(predicate1,predicateInClause)).getEntityManager().createQuery(criteriaQuery.select(cb.count(root)).where(predicate1,predicateInClause))

Something like this. 这样的事情。 I cannot give you a query, due to missing entity names. 由于缺少实体名称,我无法给您查询。 However, I recommend to read this Criteria API subselect 但是,我建议阅读此Criteria API子选择

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery cq = cb.createQuery(TABLE_VIEW_SEARCH.class);
Root root = cq.from(TABLE_VIEW_SEARCH.class);

Subquery sub = cq.subquery(TABLE_PERSON.class);
Root subRoot = sub.from(TABLE_PERSON.class);
SetJoin<TABLE_PERSON, TABLE_VIEW_SEARCH> subViewSearch = 
     subRoot.join(TABLE_PERSON.tableViewSearch);
sub.select(cb.equal(subRoot.get(TABLE_PERSON.region_ID), 1001));

cq.where(cb.equal(root.get("postal_cd"), "AL"));

TypedQuery query = em.createQuery(cq);
List result = query.getResultList();

You can use this method, once you have metamodel generation dependency declared: 一旦声明了元模型生成依赖项,就可以使用此方法:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-jpamodelgen</artifactId>
</dependency>

Rebuild your project with maven after you add this dependency, and then you can do this: 添加此依赖关系后,使用maven重建项目,然后可以执行以下操作:

EntityName_.attribute

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM