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

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

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

CriteriaQuery.getRestriction介绍

暂无

代码示例

代码示例来源:origin: katharsis-project/katharsis-framework

@Override
public void addPredicate(Predicate predicate) {
  Predicate restriction = criteriaQuery.getRestriction();
  if (restriction != null) {
    criteriaQuery.where(restriction, predicate);
  }
  else {
    criteriaQuery.where(predicate);
  }
}

代码示例来源:origin: Impetus/Kundera

Predicate where = criteriaQuery.getRestriction(); // this could be null.
if (where != null)

代码示例来源:origin: icnws/spring-data-jpa-demo

@Override
  public Predicate toPredicate(Root<MyOrder> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
    CriteriaQuery<MyOrder> q1 = cb.createQuery(MyOrder.class);
    Join<Customer, MyOrder> myOrderJoin = root.join("customer", JoinType.LEFT);
    q1.select(myOrderJoin).where(cb.equal(root.get("cId"), 1));
    Predicate p1 = q1.getRestriction();
    return p1;
  }
};

代码示例来源:origin: com.charlyghislain.authenticator/authenticator-ejb

private static <T extends WithId> Long getTotalCountResult(EntityManager entityManager, CriteriaQuery<T> query, Root<T> mainSelection) {
  CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
  CriteriaQuery<Long> countQuery = criteriaBuilder.createQuery(Long.class);
  Expression<Long> count = criteriaBuilder.count(mainSelection);
  countQuery.select(count);
  countQuery.where(query.getRestriction());
  TypedQuery<Long> countTypedQuery = entityManager.createQuery(countQuery);
  return countTypedQuery.getSingleResult();
}

代码示例来源:origin: icnws/spring-data-jpa-demo

@Override
  public Predicate toPredicate(Root<MyOrder> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
    CriteriaQuery<MyOrder> q1 = cb.createQuery(MyOrder.class);
    Join<Customer, MyOrder> myOrderJoin = root.join("customer");
    q1.select(myOrderJoin)
        .where(
            cb.equal(root.get("cId"), 1)
            , root.get("id").in(1, 2, 4)
        );
    Predicate p1 = q1.getRestriction();
    return p1;
  }
};

代码示例来源:origin: icnws/spring-data-jpa-demo

@Override
  public Predicate toPredicate(Root<MyOrder> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
    CriteriaQuery<MyOrder> q1 = cb.createQuery(MyOrder.class);
    Join<Customer, MyOrder> myOrderJoin = root.join("customer");
    q1.select(myOrderJoin)
        .where(
            cb.equal(root.get("cId"), 1),//cId=1
            cb.equal(root.get("customer").get("firstName"), "Jack")//对象customer的firstName=Jack
        );
    Predicate p1 = q1.getRestriction();
    return p1;
  }
};

代码示例来源:origin: icnws/spring-data-jpa-demo

@Override
  public Predicate toPredicate(Root<MyOrder> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
    //声明并创建MyOrder的CriteriaQuery对象
    CriteriaQuery<MyOrder> q1 = cb.createQuery(MyOrder.class);
    //连接的时候,要以声明的根查询对象(这里是root,也可以自己创建)进行join
    //Join<Z,X>是Join生成的对象,这里的Z是被连接的对象,X是目标对象,
    //  连接的属性字段是被连接的对象在目标对象的属性,这里是我们在MyOrder内声明的customer
    //join的第二个参数是可选的,默认是JoinType.INNER(内连接 inner join),也可以是JoinType.LEFT(左外连接 left join)
    Join<Customer, MyOrder> myOrderJoin = root.join("customer", JoinType.INNER);
    //用CriteriaQuery对象拼接查询条件,这里只增加了一个查询条件,cId=1
    q1.select(myOrderJoin).where(cb.equal(root.get("cId"), 1));
    //通过getRestriction获得Predicate对象
    Predicate p1 = q1.getRestriction();
    //返回对象
    return p1;
  }
};

代码示例来源:origin: 417511458/jbone

@Override
  public Predicate toPredicate(Root<RbacRoleEntity> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
    if(StringUtils.isBlank(condition)){
      return criteriaQuery.getRestriction();
    }
    Path<String> name = root.get("name");
    Path<String> title = root.get("title");
    Predicate predicate = criteriaBuilder.or(criteriaBuilder.like(name,"%" + condition + "%"),criteriaBuilder.like(title,"%" + condition + "%"));
    return predicate;
  }
}

代码示例来源:origin: 417511458/jbone

@Override
  public Predicate toPredicate(Root<RbacPermissionEntity> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
    if(StringUtils.isBlank(condition)){
      return criteriaQuery.getRestriction();
    }
    Path<String> name = root.get("name");
    Path<String> permissionValue = root.get("permissionValue");
    Predicate predicate = criteriaBuilder.or(criteriaBuilder.like(name,"%" + condition + "%"),criteriaBuilder.like(permissionValue,"%" + condition + "%"));
    return predicate;
  }
}

代码示例来源:origin: 417511458/jbone

@Override
  public Predicate toPredicate(Root<RbacSystemEntity> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
    if(StringUtils.isBlank(condition)){
      return criteriaQuery.getRestriction();
    }
    Path<String> name = root.get("name");
    Path<String> title = root.get("title");
    Path<String> basepath = root.get("basepath");
    Predicate predicate = criteriaBuilder.or(criteriaBuilder.like(name,"%" + condition + "%"),criteriaBuilder.like(title,"%" + condition + "%"),criteriaBuilder.like(basepath,"%" + condition + "%"));
    return predicate;
  }
}

代码示例来源:origin: jlfex/hermes

@Override
  public Predicate toPredicate(Root<Article> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
    List<Predicate> p = new ArrayList<Predicate>();
    if (StringUtils.isNotEmpty(levelOne) && StringUtils.isEmpty(levelTwo)) {
      p.add(cb.equal(root.<ArticleCategory> get("category").<String> get("id"), levelOne));
    }
    if (StringUtils.isNotEmpty(levelTwo) && StringUtils.isEmpty(levelThree)) {
      p.add(cb.equal(root.<ArticleCategory> get("category").<String> get("id"), levelTwo));
    }
    if (StringUtils.isNotEmpty(levelThree)) {
      p.add(cb.equal(root.<ArticleCategory> get("category").<String> get("id"), levelThree));
    }
    if (StringUtils.isNotEmpty(inputName)) {
      p.add(cb.like(root.<String> get("articleTitle"), "%" + inputName + "%"));
    }
    query.where(cb.and(p.toArray(new Predicate[p.size()])));
    return query.getRestriction();
  }
}, pageable);

代码示例来源:origin: org.fornax.cartridges/fornax-cartridges-sculptor-framework

@Override
protected void prepareResultCount(QueryConfig config) {
  CriteriaQuery<Long> resultCountCriteriaQuery = criteriaBuilder
      .createQuery(Long.class);
  // TODO: works only T = R
  if (config.isDistinct()) {
    resultCountCriteriaQuery.select(criteriaBuilder
        .countDistinct(resultCountCriteriaQuery.from(getType())));
  } else {
    resultCountCriteriaQuery.select(criteriaBuilder
        .count(resultCountCriteriaQuery.from(getType())));
  }
  if (criteriaQuery.getRestriction() != null) {
    resultCountCriteriaQuery.where(criteriaQuery.getRestriction());
  }
  resultCountQuery = getEntityManager().createQuery(
      resultCountCriteriaQuery);
};

代码示例来源:origin: 417511458/jbone

@Override
  public Predicate toPredicate(Root<RbacUserEntity> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
    if(StringUtils.isBlank(condition)){
      return criteriaQuery.getRestriction();
    }
    Path<String> username = root.get("username");
    Path<String> realname = root.get("realname");
    Path<String> phone = root.get("phone");
    Path<String> email = root.get("email");
    Predicate predicate = criteriaBuilder.or(criteriaBuilder.like(username,"%" + condition + "%"),criteriaBuilder.like(realname,"%" + condition + "%"),criteriaBuilder.like(phone,"%" + condition + "%"),criteriaBuilder.like(email,"%" + condition + "%"));
    return predicate;
  }
}

代码示例来源:origin: ZhongjunTian/spring-repository-plus

private SpecificationBuilder distinct() {
  specification.add(((root, criteriaQuery, criteriaBuilder) -> criteriaQuery.distinct(true).getRestriction()));
  return this;
}

代码示例来源:origin: org.jboss.pnc/spi

public static Predicate<BuildRecord> withAttribute(String key, String value) {
  return (root, query, cb) -> {
    MapJoin<Object, Object, Object> mapJoinAttributes = root.joinMap(BuildRecord_.attributes.getName());
    return query.where(cb.and(cb.equal(mapJoinAttributes.key(), key), cb.equal(mapJoinAttributes.value(), value))).getRestriction();
  };
}

代码示例来源:origin: com.introproventures/graphql-jpa-query-schema

/**
 * @param from
 * @param query
 */
private void applyFilters(Root<T> from, CriteriaQuery<?> query) {
  List<Predicate> predicates = new ArrayList<>();
  for (PredicateFilter filter : filters) {
    Path<?> path;
    if (filter.getCriterias().contains(PredicateFilter.Criteria.IS_NULL)) {
      path = getCompoundJoinedPath(from, filter.getField(), true);
    } else {
      path = getCompoundJoinedPath(from, filter.getField(), false);
    }
    Predicate p = predicateBuilder.getPredicate(from, path, filter);
    if (p != null) {
      predicates.add(p);
    }
  }
  // this does not work for Hibernate!!!
  if (query.getRestriction() != null) {
    predicates.add(query.getRestriction());
  }
  if (options.contains(Logical.OR)) {
    query.where(criteriaBuilder.or(predicates.toArray(new Predicate[0])));
  } else {
    query.where(criteriaBuilder.and(predicates.toArray(new Predicate[0])));
  }
}

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

public Long getRowCount(CriteriaQuery criteriaQuery,CriteriaBuilder criteriaBuilder,Root<?> root){
  CriteriaQuery<Long> countCriteria = criteriaBuilder.createQuery(Long.class);
  Root<?> entityRoot = countCriteria.from(root.getJavaType());
  entityRoot.alias(root.getAlias());
  doJoins(root.getJoins(),entityRoot);
  countCriteria.select(criteriaBuilder.count(entityRoot));
  countCriteria.where(criteriaQuery.getRestriction());
  return this.entityManager.createQuery(countCriteria).getSingleResult();
}

private void doJoins(Set<? extends Join<?, ?>> joins,Root<?> root_){
  for(Join<?,?> join: joins){
    Join<?,?> joined = root_.join(join.getAttribute().getName(),join.getJoinType());
    doJoins(join.getJoins(), joined);
  }
}

private void doJoins(Set<? extends Join<?, ?>> joins,Join<?,?> root_){
  for(Join<?,?> join: joins){
    Join<?,?> joined = root_.join(join.getAttribute().getName(),join.getJoinType());
    doJoins(join.getJoins(),joined);
  }
}

代码示例来源:origin: lordlothar99/strategy-spring-security-acl

private AclPredicateTargetSource installAclPredicateTargetSource() {
 synchronized (cachedCriteriaQuery) {
  Predicate restriction = cachedCriteriaQuery.getRestriction();
  if (restriction instanceof Advised) {
   Advised advised = (Advised) restriction;
   if (advised.getTargetSource() instanceof AclPredicateTargetSource) {
    return (AclPredicateTargetSource) advised.getTargetSource();
   }
  }
  AclPredicateTargetSource targetSource = new AclPredicateTargetSource(em.getCriteriaBuilder(), restriction);
  ProxyFactoryBean factoryBean = new ProxyFactoryBean();
  factoryBean.setTargetSource(targetSource);
  factoryBean.setAutodetectInterfaces(true);
  Predicate enhancedPredicate = (Predicate) factoryBean.getObject();
  logger.debug("ACL Jpa Specification target source initialized for criteria {}", cachedCriteriaQuery);
  // install proxy inside criteria
  cachedCriteriaQuery.where(enhancedPredicate);
  return targetSource;
 }
}

代码示例来源:origin: com.walterjwhite.infrastructure.datastore.modules/google-guice-persist-criteria-builder

/** Use ES here, it would be more efficient. */
 public void search() {
  //    criteriaBuilder.equal()
  CriteriaQuery<Long> criteriaQuery = null;
  criteriaQuery.getParameters();
  criteriaQuery.getGroupList();
  criteriaQuery.getOrderList();
  criteriaQuery.getGroupRestriction();
  criteriaQuery.getRestriction();
  criteriaQuery.getSelection();

  final Predicate predicate = null;
 }
}

代码示例来源:origin: org.jboss.pressgang.ccms/pressgang-ccms-query

/**
 * Copy Criteria without Selection
 *
 * @param from source Criteria
 * @param to   destination Criteria
 */
public static void copyCriteriaNoSelection(CriteriaQuery<?> from, CriteriaQuery<?> to) {
  // Copy Roots
  for (Root<?> root : from.getRoots()) {
    Root<?> dest = to.from(root.getJavaType());
    dest.alias(getOrCreateAlias(root));
    copyJoins(root, dest);
  }
  if (from.getGroupList() != null) to.groupBy(from.getGroupList());
  to.distinct(from.isDistinct());
  if (from.getGroupRestriction() != null) to.having(from.getGroupRestriction());
  if (from.getRestriction() != null) to.where(from.getRestriction());
  if (from.getOrderList() != null) to.orderBy(from.getOrderList());
}

相关文章