javax.persistence.criteria.CriteriaQuery.where()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(15.6k)|赞(0)|评价(0)|浏览(179)

本文整理了Java中javax.persistence.criteria.CriteriaQuery.where()方法的一些代码示例,展示了CriteriaQuery.where()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。CriteriaQuery.where()方法的具体详情如下:
包路径:javax.persistence.criteria.CriteriaQuery
类名称:CriteriaQuery
方法名:where

CriteriaQuery.where介绍

[英]Modify the query to restrict the query result according to the specified boolean expression. Replaces the previously added restriction(s), if any. This method only overrides the return type of the corresponding AbstractQuery method.
[中]修改查询以根据指定的布尔表达式限制查询结果。替换先前添加的限制(如果有)。此方法仅重写相应AbstractQuery方法的返回类型。

代码示例

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

@Override
public List<PageField> readPageFieldsByPageId(Long pageId) {
  CriteriaBuilder builder = em.getCriteriaBuilder();
  CriteriaQuery<PageField> criteria = builder.createQuery(PageField.class);
  Root<PageFieldImpl> pageField = criteria.from(PageFieldImpl.class);
  criteria.select(pageField);
  Path<Object> path = pageField.get("page").get("id");
  criteria.where(builder.equal(pageField.get("page").get("id"), pageId));
  TypedQuery<PageField> query = em.createQuery(criteria);
  query.setHint(QueryHints.HINT_CACHEABLE, true);
  return query.getResultList();
}

代码示例来源:origin: stackoverflow.com

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery cq = cb.createQuery(Employee.class);
Root emp = cq.from(Employee.class);
 <b>
 <i>
  Path<List<Project>>
 </i>
 </b>
 projects = emp.get("projects"));
cq.where(cb.isEmpty(projects);
cq.select(emp);

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

@Override
public Customer readCustomerByExternalId(String id) {
  CriteriaBuilder builder = em.getCriteriaBuilder();
  CriteriaQuery<Customer> criteria = builder.createQuery(Customer.class);
  Root<? extends Customer> customer = criteria.from(entityConfiguration.lookupEntityClass(Customer.class.getName(), Customer.class));
  criteria.select(customer);
  criteria.where(builder.equal(customer.get("externalId"), id));
  TypedQuery<Customer> query = em.createQuery(criteria);
  query.setHint(QueryHints.HINT_CACHEABLE, false);
  query.setHint(QueryHints.HINT_CACHE_REGION, "query.Customer");
  List<Customer> resultList = query.getResultList();
  return CollectionUtils.isEmpty(resultList) ? null : resultList.get(0);
}

代码示例来源:origin: spring-projects/spring-data-examples

@Override
  public void removedExpiredAccounts(LocalDate reference) {

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Account> query = cb.createQuery(Account.class);
    Root<Account> account = query.from(Account.class);

    query.where(cb.lessThan(account.get("expiryDate").as(Date.class), reference.toDateTimeAtStartOfDay().toDate()));

    for (Account each : em.createQuery(query).getResultList()) {
      em.remove(each);
    }
  }
}

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

@Override
public List<Translation> readTranslations(TranslatedEntity entity, String entityId, String fieldName) {
  entityId = getUpdatedEntityId(entity, entityId);
  CriteriaBuilder builder = em.getCriteriaBuilder();
  CriteriaQuery<Translation> criteria = builder.createQuery(Translation.class);
  Root<TranslationImpl> translation = criteria.from(TranslationImpl.class);
  criteria.select(translation);
  criteria.where(builder.equal(translation.get("entityType"), entity.getFriendlyType()),
    builder.equal(translation.get("entityId"), entityId),
    builder.equal(translation.get("fieldName"), fieldName)
  );
  TypedQuery<Translation> query = em.createQuery(criteria);
  query.setHint(QueryHints.HINT_CACHEABLE, true);
  return query.getResultList();
}

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

@Override
public Long countAllowedValuesForProductOptionById(Long productOptionId) {
  CriteriaBuilder builder = em.getCriteriaBuilder();
  CriteriaQuery<Long> criteria = builder.createQuery(Long.class);
  Root<ProductOptionValueImpl> root = criteria.from(ProductOptionValueImpl.class);
  criteria.select(builder.count(root));
  List<Predicate> restrictions = new ArrayList<>();
  List<Long> mergedIds = sandBoxHelper.mergeCloneIds(ProductOptionImpl.class, productOptionId);
  restrictions.add(root.get("productOption").in(mergedIds));
  criteria.where(restrictions.toArray(new Predicate[restrictions.size()]));
  TypedQuery<Long> query = em.createQuery(criteria);
  return query.getSingleResult();
}

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

@Override
public AdminPermission readAdminPermissionByName(String name) {
  CriteriaBuilder builder = em.getCriteriaBuilder();
  CriteriaQuery<AdminPermission> criteria = builder.createQuery(AdminPermission.class);
  Root<AdminPermissionImpl> adminPerm = criteria.from(AdminPermissionImpl.class);
  criteria.select(adminPerm);
  List<Predicate> restrictions = new ArrayList<Predicate>();
  restrictions.add(builder.equal(adminPerm.get("name"), name));
  // Execute the query with the restrictions
  criteria.where(restrictions.toArray(new Predicate[restrictions.size()]));
  TypedQuery<AdminPermission> query = em.createQuery(criteria);
  query.setHint(QueryHints.HINT_CACHEABLE, true);
  query.setHint(QueryHints.HINT_CACHE_REGION, "blAdminSecurityQuery");
  List<AdminPermission> results = query.getResultList();
  if (results == null || results.size() == 0) {
    return null;
  } else {
    return results.get(0);
  }
}

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

@Override
public AdminPermission readAdminPermissionByNameAndType(String name, String type) {
  CriteriaBuilder builder = em.getCriteriaBuilder();
  CriteriaQuery<AdminPermission> criteria = builder.createQuery(AdminPermission.class);
  Root<AdminPermissionImpl> adminPerm = criteria.from(AdminPermissionImpl.class);
  criteria.select(adminPerm);
  List<Predicate> restrictions = new ArrayList<Predicate>();
  restrictions.add(builder.equal(adminPerm.get("name"), name));
  restrictions.add(builder.equal(adminPerm.get("type"), type));
  // Execute the query with the restrictions
  criteria.where(restrictions.toArray(new Predicate[restrictions.size()]));
  TypedQuery<AdminPermission> query = em.createQuery(criteria);
  query.setHint(QueryHints.HINT_CACHEABLE, true);
  query.setHint(QueryHints.HINT_CACHE_REGION, "blAdminSecurityQuery");
  List<AdminPermission> results = query.getResultList();
  if (results == null || results.size() == 0) {
    return null;
  } else {
    return results.get(0);
  }
}

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

@Override
public List<IndexFieldType> getIndexFieldTypes(FieldType facetFieldType) {
  CriteriaBuilder builder = em.getCriteriaBuilder();
  CriteriaQuery<IndexFieldType> criteria = builder.createQuery(IndexFieldType.class);
  Root<IndexFieldTypeImpl> root = criteria.from(IndexFieldTypeImpl.class);
  criteria.select(root);
  criteria.where(
      builder.equal(root.get("fieldType").as(String.class), facetFieldType.getType())
  );
  TypedQuery<IndexFieldType> query = em.createQuery(criteria);
  query.setHint(QueryHints.HINT_CACHEABLE, true);
  query.setHint(QueryHints.HINT_CACHE_REGION, "query.Search");
  return query.getResultList();
}

代码示例来源:origin: kiegroup/jbpm

@Override
public Long findTaskIdByContentId( Long contentId ) {
  check();
  CriteriaBuilder builder = this.em.getCriteriaBuilder();
  CriteriaQuery<Long> query = builder.createQuery(Long.class);
  Root<TaskImpl> taskRoot = query.from(TaskImpl.class);
  Root<ContentImpl> contentRoot = query.from(ContentImpl.class);
  query.select(taskRoot.get(TaskImpl_.id));
  Predicate taskContentJoinPred = builder.equal(
      contentRoot.get(ContentImpl_.id),
      taskRoot.get(TaskImpl_.taskData).get(TaskDataImpl_.outputContentId));
  Predicate contentIdPred = builder.equal(
      contentRoot.get(ContentImpl_.id),
      contentId);
  query.where(builder.and(taskContentJoinPred, contentIdPred));
  Query choppedLiver = em.createQuery(query);
  return (Long) choppedLiver.getSingleResult();
}

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

@Override
public List<Page> findPageByURI(String uri) {
  CriteriaBuilder builder = em.getCriteriaBuilder();
  CriteriaQuery<Page> criteriaQuery = builder.createQuery(Page.class);
  Root pageRoot = criteriaQuery.from(PageImpl.class);
  criteriaQuery.select(pageRoot);
  
  List<Predicate> restrictions = new ArrayList<>();
  restrictions.add(builder.equal(pageRoot.get("fullUrl"), uri));
  Date currentDate = DateUtil.getCurrentDateAfterFactoringInDateResolution(cachedDate, getCurrentDateResolution());
  addActiveDateRestrictions(builder, pageRoot, restrictions, currentDate, currentDate);
  addOfflineRestriction(builder, pageRoot, restrictions);
  criteriaQuery.where(restrictions.toArray(new Predicate[restrictions.size()]));
  
  return getResultForQueryAndCache(criteriaQuery);
}

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

@Override
public List<Page> findPageByURIAndActiveDate(final String uri, final Date activeDate) {
  final CriteriaBuilder builder = em.getCriteriaBuilder();
  final CriteriaQuery<Page> criteriaQuery = builder.createQuery(Page.class);
  final Root pageRoot = criteriaQuery.from(PageImpl.class);
  criteriaQuery.select(pageRoot);
  final List<Predicate> restrictions = new ArrayList<>();
  restrictions.add(builder.equal(pageRoot.get("fullUrl"), uri));
  final Date nextDay = DateUtils.addDays(activeDate, 1);
  addOfflineRestriction(builder, pageRoot, restrictions);
  addActiveDateRestrictions(builder, pageRoot, restrictions, nextDay, activeDate);
  
  criteriaQuery.where(restrictions.toArray(new Predicate[restrictions.size()]));
  List<Page> pages = getResultForQueryAndCache(criteriaQuery);
  
  return filterInactive(pages);
}

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

@Override
public Field readFieldByAbbreviation(String abbreviation) {
  CriteriaBuilder builder = em.getCriteriaBuilder();
  CriteriaQuery<Field> criteria = builder.createQuery(Field.class);
  
  Root<FieldImpl> root = criteria.from(FieldImpl.class);
  
  criteria.select(root);
  criteria.where(
    builder.equal(root.get("abbreviation").as(String.class), abbreviation)
  );
  TypedQuery<Field> query = em.createQuery(criteria);
  query.setHint(QueryHints.HINT_CACHEABLE, true);
  query.setHint(QueryHints.HINT_CACHE_REGION, "query.Catalog");
  try {
    return query.getSingleResult();
  } catch (javax.persistence.NoResultException e) {
    //must not be an abbreviation
    return null;
  }
}

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

@Override
public Translation readTranslation(TranslatedEntity entity, String entityId, String fieldName, String localeCode) {
  entityId = getUpdatedEntityId(entity, entityId);
  CriteriaBuilder builder = em.getCriteriaBuilder();
  CriteriaQuery<Translation> criteria = builder.createQuery(Translation.class);
  Root<TranslationImpl> translation = criteria.from(TranslationImpl.class);
  criteria.select(translation);
  criteria.where(builder.equal(translation.get("entityType"), entity.getFriendlyType()),
    builder.equal(translation.get("entityId"), entityId),
    builder.equal(translation.get("fieldName"), fieldName),
    builder.equal(translation.get("localeCode"), localeCode)
  );
  TypedQuery<Translation> query = em.createQuery(criteria);
  query.setHint(QueryHints.HINT_CACHEABLE, true);
  List<Translation> translations = query.getResultList();
  if (translations.size() > 1) {
    throw new IllegalStateException("Found multiple translations for: " + entity.getFriendlyType() + "|" + entityId + "|" + fieldName + "|" + localeCode);
  }
  if (!translations.isEmpty()) {
    return translations.get(0);
  }
  return null;
}

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

@Override
public Boolean offerCodeIsUsed(OfferCode code) {
  CriteriaBuilder builder = em.getCriteriaBuilder();
  CriteriaQuery<Order> criteria = builder.createQuery(Order.class);
  Root<OrderImpl> baseOrder = criteria.from(OrderImpl.class);
  criteria.select(baseOrder);
  Join<OrderImpl, OfferCodeImpl> join = baseOrder.join("addedOfferCodes");
  criteria.where(builder.equal(join.get("id"), code.getId()));
  TypedQuery<Order> query = em.createQuery(criteria);
  try {
    query.getSingleResult();
  } catch (NoResultException e) {
    return false;
  }
  return true;
}

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

@Override
public List<Order> readOrdersByDateRange(final Date startDate, final Date endDate) {
  CriteriaBuilder builder = em.getCriteriaBuilder();
  CriteriaQuery<Order> criteria = builder.createQuery(Order.class);
  Root<OrderImpl> order = criteria.from(OrderImpl.class);
  criteria.select(order);
  criteria.where(builder.between(order.<Date>get("submitDate"), startDate, endDate));
  criteria.orderBy(builder.desc(order.get("submitDate")));
  TypedQuery<Order> query = em.createQuery(criteria);
  query.setHint(QueryHints.HINT_CACHEABLE, true);
  query.setHint(QueryHints.HINT_CACHE_REGION, "query.Order");
  return query.getResultList();
}

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

@Override
public List<IndexField> readFieldsByEntityType(FieldEntity entityType) {
  CriteriaBuilder builder = em.getCriteriaBuilder();
  CriteriaQuery<IndexField> criteria = builder.createQuery(IndexField.class);
  Root<IndexFieldImpl> root = criteria.from(IndexFieldImpl.class);
  
  criteria.select(root);
  criteria.where(root.get("field").get("entityType").as(String.class).in(entityType.getAllLookupTypes()));
  TypedQuery<IndexField> query = em.createQuery(criteria);
  query.setHint(QueryHints.HINT_CACHEABLE, true);
  query.setHint(QueryHints.HINT_CACHE_REGION, "query.Search");
  return query.getResultList();
}

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

@Override
public List<Field> readFieldsByEntityType(FieldEntity entityType) {
  CriteriaBuilder builder = em.getCriteriaBuilder();
  CriteriaQuery<Field> criteria = builder.createQuery(Field.class);
  Root<FieldImpl> root = criteria.from(FieldImpl.class);
  criteria.select(root);
  criteria.where(
      root.get("entityType").as(String.class).in(entityType.getAllLookupTypes())
      );
  TypedQuery<Field> query = em.createQuery(criteria);
  query.setHint(QueryHints.HINT_CACHEABLE, true);
  query.setHint(QueryHints.HINT_CACHE_REGION, "query.Catalog");
  return query.getResultList();
}

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

@Override
public List<Customer> readCustomersByIds(List<Long> ids){
  if (ids == null || ids.size() == 0) {
    return null;
  }
  if (ids.size() > 100) {
    LOG.warn("Not recommended to use the readCustomersByIds method for long lists of customerIds, since " +
        "Hibernate is required to transform the distinct results. The list of requested" +
        "customer ids was (" + ids.size() + ") in length.");
  }
  CriteriaBuilder builder = em.getCriteriaBuilder();
  CriteriaQuery<Customer> criteria = builder.createQuery(Customer.class);
  Root<CustomerImpl> customer = criteria.from(CustomerImpl.class);
  criteria.select(customer);
  // We only want results that match the customer IDs
  criteria.where(customer.get("id").as(Long.class).in(ids));
  TypedQuery<Customer> query = em.createQuery(criteria);
  query.setHint(QueryHints.HINT_CACHEABLE, true);
  query.setHint(QueryHints.HINT_CACHE_REGION, "query.Order");
  return query.getResultList();
}

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

@Override
public List<Order> readOrdersByIds(List<Long> orderIds) {
  if (orderIds == null || orderIds.size() == 0) {
    return null;
  }
  if (orderIds.size() > 100) {
    LOG.warn("Not recommended to use the readOrdersByIds method for long lists of orderIds, since " +
        "Hibernate is required to transform the distinct results. The list of requested" +
        "order ids was (" + orderIds.size() + ") in length.");
  }
  CriteriaBuilder builder = em.getCriteriaBuilder();
  CriteriaQuery<Order> criteria = builder.createQuery(Order.class);
  Root<OrderImpl> order = criteria.from(OrderImpl.class);
  criteria.select(order);
  // We only want results that match the order IDs
  criteria.where(order.get("id").as(Long.class).in(orderIds));
  TypedQuery<Order> query = em.createQuery(criteria);
  query.setHint(QueryHints.HINT_CACHEABLE, true);
  query.setHint(QueryHints.HINT_CACHE_REGION, "query.Order");
  return query.getResultList();
}

相关文章