Java Code Examples for org.hibernate.criterion.Restrictions#or()

The following examples show how to use org.hibernate.criterion.Restrictions#or() . 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: HibernateCriterionUtils.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
public static Criterion CriterionInRestrictionSplitter(String property, Collection<?> values) {
    Objects.requireNonNull(property);
    Objects.requireNonNull(values);

    Criterion criterion = null;
    List<?> list = new ArrayList<>(values);
    int listSize = list.size();

    for (int i = 0; i < listSize; i += MAX_NUMBER_OF_SQL_PARAMETERS_IN_LIST) {
        List<?> subList;
        if (listSize > i + MAX_NUMBER_OF_SQL_PARAMETERS_IN_LIST) {
            subList = list.subList(i, (i + MAX_NUMBER_OF_SQL_PARAMETERS_IN_LIST));
        } else {
            subList = list.subList(i, listSize);
        }
        if (criterion == null) {
            criterion = Restrictions.in(property, subList);
        } else {
            criterion = Restrictions.or(criterion, Restrictions.in(property, subList));
        }
    }
    return criterion;
}
 
Example 2
Source File: HibernateCriterionUtils.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
public static Criterion CriterionInRestrictionSplitter(String property, Collection<?> values) {
    Objects.requireNonNull(property);
    Objects.requireNonNull(values);

    Criterion criterion = null;
    List<?> list = new ArrayList<>(values);
    int listSize = list.size();

    for (int i = 0; i < listSize; i += MAX_NUMBER_OF_SQL_PARAMETERS_IN_LIST) {
        List<?> subList;
        if (listSize > i + MAX_NUMBER_OF_SQL_PARAMETERS_IN_LIST) {
            subList = list.subList(i, (i + MAX_NUMBER_OF_SQL_PARAMETERS_IN_LIST));
        } else {
            subList = list.subList(i, listSize);
        }
        if (criterion == null) {
            criterion = Restrictions.in(property, subList);
        } else {
            criterion = Restrictions.or(criterion, Restrictions.in(property, subList));
        }
    }
    return criterion;
}
 
Example 3
Source File: CollectionRepositoryImpl.java    From AIDR with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public Long getStoppedCollectionsCount(String terms) {
	Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(Collection.class);
	criteria.setProjection(Projections.projectionList()
			.add(Projections.property("id"), "id"));
	
	LogicalExpression or = Restrictions.or(
			Restrictions.eq("status", CollectionStatus.STOPPED),
			Restrictions.eq("status", CollectionStatus.NOT_RUNNING)
			);
	
	LogicalExpression orAll = Restrictions.or(
			or,
			Restrictions.eq("status", CollectionStatus.FATAL_ERROR)
			);
	
	criteria.add(orAll);
	addCollectionSearchCriteria(terms, criteria);

	ScrollableResults scroll = criteria.scroll();
	int i = scroll.last() ? scroll.getRowNumber() + 1 : 0;
	return Long.valueOf(i);
}
 
Example 4
Source File: CollectionRepositoryImpl.java    From AIDR with GNU Affero General Public License v3.0 6 votes vote down vote up
private void addCollectionSearchCriteria(String terms, Criteria criteria) {
	if (StringUtils.hasText(terms)){
		String wildcard ='%'+ URLDecoder.decode(terms.trim())+'%';

		LogicalExpression orNameCode = Restrictions.or(
				Restrictions.ilike("name", wildcard),
				Restrictions.ilike("code", wildcard)
				);

		LogicalExpression orAll = Restrictions.or(
				orNameCode,
				Restrictions.ilike("track", wildcard)
				);

		criteria.add(orAll);
	}
}
 
Example 5
Source File: CategoryCriterion.java    From ctsms with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static Criterion getCategoryCriterionRestriction(CategoryCriterion categoryCriterion) {
	Criterion restriction = null;
	if (categoryCriterion.prefix != null && categoryCriterion.prefix.length() > 0) {
		if (categoryCriterion.caseSensitive) {
			restriction = Restrictions.like(categoryCriterion.field, categoryCriterion.prefix, categoryCriterion.matchMode);
		} else {
			restriction = Restrictions.ilike(categoryCriterion.field, categoryCriterion.prefix, categoryCriterion.matchMode);
		}
	} else if (EmptyPrefixModes.NON_EMPTY_ROWS.equals(categoryCriterion.emptyPrefixMode)) {
		restriction = Restrictions.not(Restrictions.or(Restrictions.eq(categoryCriterion.field, ""), Restrictions.isNull(categoryCriterion.field)));
	} else if (EmptyPrefixModes.EMPTY_ROWS.equals(categoryCriterion.emptyPrefixMode)) {
		restriction = Restrictions.or(Restrictions.eq(categoryCriterion.field, ""), Restrictions.isNull(categoryCriterion.field));
	}
	return restriction;
}
 
Example 6
Source File: SQLVisitor.java    From DataHubSystem with GNU Affero General Public License v3.0 5 votes vote down vote up
private Criterion getCriterionLogical(BinaryOperator operator,
      Criterion left, Criterion right)
{
   Criterion criterion;
   if (left == null && right == null)
   {
      criterion = null;
   }
   else if (left != null && right != null)
   {
      switch (operator)
      {
         case AND:
         {
            criterion = Restrictions.and(left, right);
            break;
         }
         case OR:
         {
            criterion = Restrictions.or(left, right);
            break;
         }
         default:
         {
            throw new UnsupportedOperationException(
                  "Unsupported operator: " + operator.toUriLiteral());
         }
      }
   }
   else if (left == null)
   {
      criterion = right;
   }
   else
   {
      criterion = left;
   }
   return criterion;
}
 
Example 7
Source File: CollectionRepositoryImpl.java    From AIDR with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Collection getRunningCollectionStatusByUser(Long userId) {
	Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(Collection.class);
	//criteria.add(Restrictions.eq("user.id", userId));
	//criteria.add(Restrictions.eq("status", CollectionStatus.RUNNING));
	
	LogicalExpression or = Restrictions.or(
			Restrictions.eq("status", CollectionStatus.RUNNING),
			Restrictions.eq("status", CollectionStatus.RUNNING_WARNING)				
			);
	
	LogicalExpression orAll = Restrictions.or(
			or,
			Restrictions.eq("status", CollectionStatus.WARNING)
			);
	
	/*Is this check needed?
	 * 
	 * LogicalExpression and = Restrictions.and(
			orAll,
			Restrictions.ne("status", CollectionStatus.TRASHED)				
			);*/
	LogicalExpression andAll = Restrictions.and(
			orAll,
			Restrictions.eq("owner.id", userId)
			);
	
	criteria.add(andAll);
	//criteria.add(Restrictions.ne("status", CollectionStatus.TRASHED));
	return (Collection) criteria.uniqueResult();
}
 
Example 8
Source File: CollectionRepositoryImpl.java    From AIDR with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Long getRunningCollectionsCount(String terms) {
	Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(Collection.class);
	criteria.setProjection(Projections.projectionList()
			.add(Projections.property("id"), "id"));

	LogicalExpression or = Restrictions.or(
			Restrictions.eq("status", CollectionStatus.RUNNING),
			Restrictions.eq("status", CollectionStatus.RUNNING_WARNING)
			);

	LogicalExpression or2 = Restrictions.or(
			or,
			Restrictions.eq("status", CollectionStatus.INITIALIZING)
			);
	
	LogicalExpression orAll = Restrictions.or(
			or2,
			Restrictions.eq("status", CollectionStatus.WARNING)
			);

	LogicalExpression andAll = Restrictions.and(
			orAll,
			Restrictions.ne("status", CollectionStatus.TRASHED)
			);

	criteria.add(andAll);
	addCollectionSearchCriteria(terms, criteria);

	ScrollableResults scroll = criteria.scroll();
	int i = scroll.last() ? scroll.getRowNumber() + 1 : 0;
	return Long.valueOf(i);
}
 
Example 9
Source File: CollectionRepositoryImpl.java    From AIDR with GNU Affero General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public List<Collection> getRunningCollections(Integer start, Integer limit, String terms, String sortColumn, String sortDirection) {
	Criteria criteriaIds = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(Collection.class);
	criteriaIds.setProjection(Projections.projectionList()
			.add(Projections.property("id"), "id"));

	LogicalExpression or = Restrictions.or(
			Restrictions.eq("status", CollectionStatus.RUNNING),
			Restrictions.eq("status", CollectionStatus.RUNNING_WARNING)
			);

	LogicalExpression or2 = Restrictions.or(
			or,
			Restrictions.eq("status", CollectionStatus.INITIALIZING)
			);
	
	LogicalExpression orAll = Restrictions.or(
			or2,
			Restrictions.eq("status", CollectionStatus.WARNING)
			);
	
	LogicalExpression andAll = Restrictions.and(
			orAll,
			Restrictions.ne("status", CollectionStatus.TRASHED)
			);

	criteriaIds.add(andAll);
	addCollectionSearchCriteria(terms, criteriaIds);
	searchCollectionsAddOrder(sortColumn, sortDirection, criteriaIds);

	if (start != null) {
		criteriaIds.setFirstResult(start);
	}
	if (limit != null) {
		criteriaIds.setMaxResults(limit);
	}

	List<Integer> ids = criteriaIds.list();

	if (ids.size() == 0){
		return Collections.emptyList();
	}

	Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(Collection.class);
	criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);

	criteria.add(Restrictions.in("id", ids));
	searchCollectionsAddOrder(sortColumn, sortDirection, criteria);

	return criteria.list();
}
 
Example 10
Source File: CollectionRepositoryImpl.java    From AIDR with GNU Affero General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public List<Collection> getStoppedCollections(Integer start, Integer limit, String terms, String sortColumn, String sortDirection) {
	Criteria criteriaIds = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(Collection.class);
	criteriaIds.setProjection(Projections.projectionList()
			.add(Projections.property("id"), "id"));

	LogicalExpression or = Restrictions.or(
			Restrictions.eq("status", CollectionStatus.STOPPED),
			Restrictions.eq("status", CollectionStatus.NOT_RUNNING)
			);
	
	LogicalExpression orAll = Restrictions.or(
			or,
			Restrictions.eq("status", CollectionStatus.FATAL_ERROR)
			);
	
	criteriaIds.add(orAll);
	addCollectionSearchCriteria(terms, criteriaIds);
	searchCollectionsAddOrder(sortColumn, sortDirection, criteriaIds);

	if (start != null) {
		criteriaIds.setFirstResult(start);
	}
	if (limit != null) {
		criteriaIds.setMaxResults(limit);
	}

	List<Integer> ids = criteriaIds.list();

	if (ids.size() == 0){
		return Collections.emptyList();
	}

	Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(Collection.class);
	criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);

	criteria.add(Restrictions.in("id", ids));
	searchCollectionsAddOrder(sortColumn, sortDirection, criteria);

	return criteria.list();
}
 
Example 11
Source File: CriteriaQuery.java    From jeewx with Apache License 2.0 2 votes vote down vote up
/**
 * 设置组合后的Criterion OR关系
 * 
 * @param query
 * @param source
 * @param dest
 * @return
 */
public Criterion getor(Criterion c1,Criterion c2) {
	return Restrictions.or(c1, c2);
}
 
Example 12
Source File: CriteriaQuery.java    From jeewx with Apache License 2.0 2 votes vote down vote up
/**
 * 设置Or查询
 * 
 * @param query
 * @param source条件1
 * @param dest条件2
 * @return
 */
public Criterion or(CriteriaQuery query, int source, int dest) {
	return Restrictions.or(query.getCriterionList().getParas(source), query
			.getCriterionList().getParas(dest));
}
 
Example 13
Source File: CriteriaQuery.java    From jeewx with Apache License 2.0 2 votes vote down vote up
/**
 * 设置or(Criterion c, CriteriaQuery query, int source)(或)查询条件
 * 
 * @param keyname
 * @param keyvalue1
 * @param keyvalue2
 */
public Criterion or(Criterion c, CriteriaQuery query, int source) {
	return Restrictions.or(c, query.getCriterionList().getParas(source));
}
 
Example 14
Source File: CriteriaQuery.java    From jeecg with Apache License 2.0 2 votes vote down vote up
/**
 * 设置组合后的Criterion OR关系
 * 
 * @param query
 * @param source
 * @param dest
 * @return
 */
public Criterion getor(Criterion c1,Criterion c2) {
	return Restrictions.or(c1, c2);
}
 
Example 15
Source File: CriteriaQuery.java    From jeecg with Apache License 2.0 2 votes vote down vote up
/**
 * 设置Or查询
 * 
 * @param query
 * @param source条件1
 * @param dest条件2
 * @return
 */
public Criterion or(CriteriaQuery query, int source, int dest) {
	return Restrictions.or(query.getCriterionList().getParas(source), query
			.getCriterionList().getParas(dest));
}
 
Example 16
Source File: CriteriaQuery.java    From jeecg with Apache License 2.0 2 votes vote down vote up
/**
 * 设置or(Criterion c, CriteriaQuery query, int source)(或)查询条件
 * 
 * @param keyname
 * @param keyvalue1
 * @param keyvalue2
 */
public Criterion or(Criterion c, CriteriaQuery query, int source) {
	return Restrictions.or(c, query.getCriterionList().getParas(source));
}