Java Code Examples for javax.persistence.criteria.CriteriaQuery#getRestriction()

The following examples show how to use javax.persistence.criteria.CriteriaQuery#getRestriction() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TaskDataSourceDaoImpl.java    From Qualitis with Apache License 2.0 6 votes vote down vote up
private Specification<TaskDataSource> getUserAndDataSourceSpecification(String username, String clusterName, String databaseName, String tableName) {
    return new Specification<TaskDataSource>() {
        @Override
        public Predicate toPredicate(Root<TaskDataSource> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
            List<Predicate> predicates = new ArrayList<>();
            if (clusterName != null) {
                predicates.add(criteriaBuilder.equal(root.get("clusterName"), clusterName));
            }
            if (databaseName != null) {
                predicates.add(criteriaBuilder.equal(root.get("databaseName"), databaseName));
            }
            if (tableName != null) {
                predicates.add(criteriaBuilder.equal(root.get("tableName"), tableName));
            }
            predicates.add(criteriaBuilder.equal(root.get("createUser"), username));

            Predicate[] p = new Predicate[predicates.size()];
            query.where(criteriaBuilder.and(predicates.toArray(p)));

            return query.getRestriction();
        }
    };
}
 
Example 2
Source File: RdbmsUtils.java    From modeldb with Apache License 2.0 6 votes vote down vote up
/**
 * Return the data count base on the criteria query
 *
 * @param session : hibernate session
 * @param root : entity root which is further used for getting sub filed path from it and set in
 *     criteria where clause. Ex: Root<ProjectEntity> projectRoot =
 *     criteriaQuery.from(ProjectEntity.class);
 * @param criteria : Hibernate criteria query reference for further process
 * @param <T> : T = entity name like ProjectEntity, DatasetEntity, ExperimentEntity etc.
 * @return {@link Long} : total records count
 */
public static <T> long count(Session session, Root<T> root, CriteriaQuery<T> criteria) {
  final CriteriaBuilder builder = session.getCriteriaBuilder();
  final CriteriaQuery<Long> countCriteria = builder.createQuery(Long.class);

  countCriteria.select(builder.count(root));
  countCriteria.getRoots().addAll(criteria.getRoots());

  final Predicate whereRestriction = criteria.getRestriction();
  if (whereRestriction != null) {
    countCriteria.where(whereRestriction);
  }

  final Predicate groupRestriction = criteria.getGroupRestriction();
  if (groupRestriction != null) {
    countCriteria.having(groupRestriction);
  }

  countCriteria.groupBy(criteria.getGroupList());
  countCriteria.distinct(criteria.isDistinct());
  return session.createQuery(countCriteria).getSingleResult();
}
 
Example 3
Source File: JpaUtil.java    From linq with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T> TypedQuery<Long> getCountQuery(CriteriaQuery<T> cq) {
	Class<T> domainClass = cq.getResultType();
	EntityManager em = getEntityManager(domainClass);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<Long> countCq = cb.createQuery(Long.class);
	Root<T> root;
	if (cq.getRestriction() != null) {
		countCq.where(cq.getRestriction());
	}
	if (cq.getGroupRestriction() != null) {
		countCq.having(cq.getGroupRestriction());
	}
	if (cq.getRoots().isEmpty()) {
		root = countCq.from(domainClass);
	} else {
		countCq.getRoots().addAll(cq.getRoots());
		root = (Root<T>) countCq.getRoots().iterator().next();
	}
	countCq.groupBy(cq.getGroupList());
	if (cq.isDistinct()) {
		countCq.select(cb.countDistinct(root));
	} else {
		countCq.select(cb.count(root));
	}

	return em.createQuery(countCq);
}
 
Example 4
Source File: JpaUtils.java    From jdal with Apache License 2.0 5 votes vote down vote up
/**
 * Copy criteria without selection and order.
 * @param from source Criteria.
 * @param to destination Criteria.
 */
private static void copyCriteriaWithoutSelectionAndOrder(
		CriteriaQuery<?> from, CriteriaQuery<?> to, boolean copyFetches) {
	if (isEclipseLink(from) && from.getRestriction() != null) {
		// EclipseLink adds roots from predicate paths to critera. Skip copying 
		// roots as workaround.
	}
	else {
		 // Copy Roots
		 for (Root<?> root : from.getRoots()) {
			 Root<?> dest = to.from(root.getJavaType());
			 dest.alias(getOrCreateAlias(root));
			 copyJoins(root, dest);
			 if (copyFetches)
				 copyFetches(root, dest);
		 }
	}
	
	to.groupBy(from.getGroupList());
	to.distinct(from.isDistinct());
	
	if (from.getGroupRestriction() != null)
		to.having(from.getGroupRestriction());
	
	Predicate predicate = from.getRestriction();
	if (predicate != null)
		to.where(predicate);
}
 
Example 5
Source File: ServiceOwnerService.java    From spring-cloud-gray with Apache License 2.0 4 votes vote down vote up
public Page<ServiceOwner> queryServiceOwners(ServiceOwnerQuery serviceOwnerQuery, Pageable pageable) {
//        QServiceOwnerDO qServiceOwnerDO = QServiceOwnerDO.serviceOwnerDO;
//        Predicate predicate = null;
//        switch (queryRecords.getQueryItem()){
//            case ServiceOwnerQuery.QUERY_ITEM_BINDED:
//                predicate = qServiceOwnerDO.userId.isNotNull();
//            case ServiceOwnerQuery.QUERY_ITEM_UNBINDED:
//                predicate = qServiceOwnerDO.userId.isNotNull();
//        }
//        if(StringUtils.isNotEmpty(queryRecords.getServiceId())){
//
//        }

        Specification<ServiceOwnerDO> specification = new Specification<ServiceOwnerDO>() {

            @Override
            public Predicate toPredicate(Root<ServiceOwnerDO> root, CriteriaQuery<?> query, CriteriaBuilder cb) {

                List<Predicate> predicates = new ArrayList();

                switch (serviceOwnerQuery.getQueryItem()) {
                    case ServiceOwnerQuery.QUERY_ITEM_BINDED:
//                        predicates.add(cb.isNotNull(root.get("userId").as(String.class)));
                        Predicate p1 = cb.isNotNull(root.get("userId"));
                        Predicate p2 = cb.notEqual(root.get("userId").as(String.class), "");
                        predicates.add(cb.and(p1,p2));
                        break;
                    case ServiceOwnerQuery.QUERY_ITEM_UNBINDED:
                        Predicate unbindP1 = cb.isNull(root.get("userId").as(String.class));
                        Predicate unbindP2 = cb.equal(root.get("userId").as(String.class), "");
                        predicates.add(cb.or(unbindP1, unbindP2));
                        break;
                }
                if(StringUtils.isNotEmpty(serviceOwnerQuery.getServiceId())){
                    predicates.add(cb.equal(root.get("serviceId").as(String.class), serviceOwnerQuery.getServiceId()));
                }
                query.where(predicates.toArray(new Predicate[predicates.size()]));
                return query.getRestriction();
//                return cb.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };

        Page<ServiceOwnerDO> doPage = repository.findAll(specification, pageable);
        return PaginationUtils.convert(pageable, doPage, getModelMapper());
    }