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

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

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

CriteriaQuery.select介绍

[英]Specify the item that is to be returned in the query result. Replaces the previously specified selection(s), if any.

Note: Applications using the string-based API may need to specify the type of the select item when it results from a get or join operation and the query result type is specified.

For example: 
CriteriaQuery<String> q = cb.createQuery(String.class); 
Root<Order> order = q.from(Order.class); 
q.select(order.get("shippingAddress").<String>get("state")); 
CriteriaQuery<Product> q2 = cb.createQuery(Product.class); 
q2.select(q2.from(Order.class) 
.join("items") 
.<Item,Product>join("product"));

[中]指定要在查询结果中返回的项。替换以前指定的选择(如果有)。
注意:使用基于字符串的API的应用程序可能需要指定从get或join操作生成的select项的类型,并且指定了查询结果类型。

For example: 
CriteriaQuery<String> q = cb.createQuery(String.class); 
Root<Order> order = q.from(Order.class); 
q.select(order.get("shippingAddress").<String>get("state")); 
CriteriaQuery<Product> q2 = cb.createQuery(Product.class); 
q2.select(q2.from(Order.class) 
.join("items") 
.<Item,Product>join("product"));

代码示例

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

public List<User> myCustomBatchOperation() {

    CriteriaQuery<User> criteriaQuery = em.getCriteriaBuilder().createQuery(User.class);
    criteriaQuery.select(criteriaQuery.from(User.class));
    return em.createQuery(criteriaQuery).getResultList();
  }
}

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

@Override
public Long readNumberOfAddresses() {
  CriteriaBuilder builder = em.getCriteriaBuilder();
  CriteriaQuery<Long> criteria = builder.createQuery(Long.class);
  criteria.select(builder.count(criteria.from(CustomerAddressImpl.class)));
  TypedQuery<Long> query = em.createQuery(criteria);
  return query.getSingleResult();
}

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

@Override
public Long readNumberOfOrderItems() {
  CriteriaBuilder builder = em.getCriteriaBuilder();
  CriteriaQuery<Long> criteria = builder.createQuery(Long.class);
  criteria.select(builder.count(criteria.from(OrderItemImpl.class)));
  TypedQuery<Long> query = em.createQuery(criteria);
  return query.getSingleResult();    }

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

@Override
  public Long readNumberOfOrders() {
     CriteriaBuilder builder = em.getCriteriaBuilder();
     CriteriaQuery<Long> criteria = builder.createQuery(Long.class);
     criteria.select(builder.count(criteria.from(OrderImpl.class)));
     TypedQuery<Long> query = em.createQuery(criteria);
     return query.getSingleResult();
  }
}

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

public long count(Class<?> clazz) {
  CriteriaBuilder cb = em.getCriteriaBuilder();
  CriteriaQuery<Long> cq = cb.createQuery(Long.class);
  cq.select(cb.count(cq.from(clazz)));
  return em.createQuery(cq).getSingleResult();
}

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

@Override
  public Long readNumberOfCustomers() {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Long> criteria = builder.createQuery(Long.class);
    criteria.select(builder.count(criteria.from(CustomerImpl.class)));
    TypedQuery<Long> query = em.createQuery(criteria);

    return query.getSingleResult();
  }
}

代码示例来源: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 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 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: BroadleafCommerce/BroadleafCommerce

@Override
public List<StructuredContent> findAllContentItems() {
  CriteriaBuilder builder = em.getCriteriaBuilder();
  CriteriaQuery<StructuredContent> criteria = builder.createQuery(StructuredContent.class);
  Root<StructuredContentImpl> sc = criteria.from(StructuredContentImpl.class);
  criteria.select(sc);
  try {
    TypedQuery<StructuredContent> query = em.createQuery(criteria);
    query.setHint(QueryHints.HINT_CACHEABLE, true);
    return query.getResultList();
  } catch (NoResultException e) {
    return new ArrayList<StructuredContent>();
  }
}

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

@Override
public List<URLHandler> findAllURLHandlers() {
  CriteriaBuilder builder = em.getCriteriaBuilder();
  CriteriaQuery<URLHandler> criteria = builder.createQuery(URLHandler.class);
  Root<URLHandlerImpl> handler = criteria.from(URLHandlerImpl.class);
  criteria.select(handler);
  TypedQuery<URLHandler> query = em.createQuery(criteria);
  query.setHint(QueryHints.HINT_CACHEABLE, true);
  try {
    return query.getResultList();
  } catch (NoResultException e) {
    return new ArrayList<URLHandler>();
  }
}

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

@Override
public List<Page> readAllPages() {
  CriteriaBuilder builder = em.getCriteriaBuilder();
  CriteriaQuery<Page> criteria = builder.createQuery(Page.class);
  Root<PageImpl> page = criteria.from(PageImpl.class);
  criteria.select(page);
  try {
    Query query = em.createQuery(criteria);
    query.setHint(QueryHints.HINT_CACHEABLE, true);
    return query.getResultList();
  } catch (NoResultException e) {
    return Collections.emptyList();
  }
}

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

@Override
public List<PageTemplate> readAllPageTemplates() {
  CriteriaBuilder builder = em.getCriteriaBuilder();
  CriteriaQuery<PageTemplate> criteria = builder.createQuery(PageTemplate.class);
  Root<PageTemplateImpl> template = criteria.from(PageTemplateImpl.class);
  criteria.select(template);
  try {
    Query query = em.createQuery(criteria);
    query.setHint(QueryHints.HINT_CACHEABLE, true);
    return query.getResultList();
  } catch (NoResultException e) {
    return Collections.emptyList();
  }
}

代码示例来源: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 readTotalStaticAssetCount() {
  CriteriaBuilder builder = em.getCriteriaBuilder();
  CriteriaQuery<Long> criteria = builder.createQuery(Long.class);
  criteria.select(builder.count(criteria.from(StaticAssetImpl.class)));
  TypedQuery<Long> query = em.createQuery(criteria);
  query.setHint(QueryHints.HINT_CACHEABLE, true);
  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();
}

相关文章