List of usage examples for org.hibernate.criterion Restrictions like
public static SimpleExpression like(String propertyName, String value, MatchMode matchMode)
From source file:com.abiquo.server.core.config.SystemPropertyDAO.java
License:Open Source License
private Criterion equalsComponent(String component) { String prefix = component + "."; return Restrictions.like(SystemProperty.NAME_PROPERTY, prefix, MatchMode.START); }
From source file:com.abssh.util.GenericDao.java
License:Apache License
@SuppressWarnings("unchecked") public Page<T> findPageDynamicFetch(final Page<T> page, final List<PropertyFilter> filters, final String... lazyObjects) { Criteria criteria = getSession().createCriteria(entityClass); Map<String, Criteria> criteriaMap = new HashMap<String, Criteria>(); for (PropertyFilter filter : filters) { if (!MatchType.INS.equals(filter.getMatchType())) { if (!filter.isMultiProperty()) { String propertyName = filter.getPropertyName(); Object[] propertyValue = filter.getPropertyValue(); MatchType matchType = filter.getMatchType(); Criteria parent = findParentCriteria(criteria, propertyName, criteriaMap); String[] tmp = StringUtils.split(propertyName, DEF_SEPARATOR); parent.add(getCriterion(tmp[tmp.length - 1], propertyValue, matchType)); } else { Disjunction disjunction = Restrictions.disjunction(); Object[] propertyValue = filter.getPropertyValue(); MatchType matchType = filter.getMatchType(); String[] propertyNames = filter.getPropertyNames(); for (String propertyName : propertyNames) { // Criteria parent = findParentCriteria(criteria, // propertyName, criteriaMap); String[] tmp = StringUtils.split(propertyName, DEF_SEPARATOR); // parent.add(getCriterion(tmp[tmp.length - 1], // propertyValue, matchType)); for (int i = 0; i <= tmp.length - 2; i++) { criteria.createAlias(tmp[i], tmp[i], CriteriaSpecification.LEFT_JOIN); }//from w w w.j a va2 s. c om // disjunction.ad } criteria.add(Restrictions.or( Restrictions.like(propertyNames[0], propertyValue[0].toString(), MatchMode.ANYWHERE), Restrictions.like(propertyNames[1], propertyValue[0].toString(), MatchMode.ANYWHERE))); // criteria.add(disjunction); } } else { criteria.add(org.hibernate.criterion.Expression.sql("this_." + filter.getPropertyName() + " in " + String.valueOf(filter.getPropertyValue()[0]))); } } if (lazyObjects != null) { for (int i = 0; i < lazyObjects.length; i++) { criteria.setFetchMode(lazyObjects[i], FetchMode.EAGER); } } if (page != null && page.isAutoCount()) { int totalCount = countCriteriaResult(criteria); page.setTotalCount(totalCount); } if (page != null && page.getPageSize() > 0) { if (page.getTotalPages() < page.getPageNo()) { page.setPageNo(1L); } criteria.setFirstResult(page.getFirst() - 1); criteria.setMaxResults(page.getPageSize()); } if (page != null && page.isOrderBySetted()) { String[] orderByArray = StringUtils.split(page.getOrderBy(), ','); String[] orderArray = StringUtils.split(page.getOrder(), ','); Assert.isTrue(orderByArray.length == orderArray.length, "orderBy and order is not suited!"); for (int i = 0; i < orderByArray.length; i++) { if (orderByArray[i].indexOf(".") > 0) { // ??? if (Page.ASC.equals(orderArray[i])) { Criteria p = criteriaMap .get(orderByArray[i].substring(0, orderByArray[i].lastIndexOf("."))); if (p == null) { p = findParentCriteria(criteria, orderByArray[i], criteriaMap);// ?? } p.addOrder(Order.asc(orderByArray[i].substring(orderByArray[i].lastIndexOf(".") + 1))); } else { Criteria p = criteriaMap .get(orderByArray[i].substring(0, orderByArray[i].lastIndexOf("."))); if (p == null) { p = findParentCriteria(criteria, orderByArray[i], criteriaMap);// ?? } p.addOrder(Order.desc(orderByArray[i].substring(orderByArray[i].lastIndexOf(".") + 1))); } } else { if (Page.ASC.equals(orderArray[i])) { criteria.addOrder(Order.asc(orderByArray[i])); } else { criteria.addOrder(Order.desc(orderByArray[i])); } } } } List result = criteria.list(); if (page == null) { Page p = new Page<T>(); p.setResult(result); p.setTotalCount(result.size()); p.setPageNo(1L); p.setPageSize(result.size()); return p; } page.setResult(result); return page; }
From source file:com.abssh.util.GenericDao.java
License:Apache License
/** * ??Criterion,.//from w ww.j a va2 s .c om */ protected Criterion buildPropertyFilterCriterion(final String propertyName, final Object[] propertyValue, final MatchType matchType) { Assert.hasText(propertyName, "propertyName should not be null!"); Criterion criterion = null; try { // ?MatchTypecriterion if (MatchType.EQ.equals(matchType)) { criterion = Restrictions.eq(propertyName, propertyValue[0]); } else if (MatchType.LIKE.equals(matchType)) { criterion = Restrictions.like(propertyName, (String) propertyValue[0], MatchMode.ANYWHERE); } else if (MatchType.ELIKE.equals(matchType)) { criterion = Restrictions.like(propertyName, (String) propertyValue[0], MatchMode.END); } else if (MatchType.SLIKE.equals(matchType)) { criterion = Restrictions.like(propertyName, (String) propertyValue[0], MatchMode.START); } else if (MatchType.LE.equals(matchType)) { criterion = Restrictions.le(propertyName, propertyValue[0]); } else if (MatchType.LT.equals(matchType)) { criterion = Restrictions.lt(propertyName, propertyValue[0]); } else if (MatchType.GE.equals(matchType)) { criterion = Restrictions.ge(propertyName, propertyValue[0]); } else if (MatchType.GT.equals(matchType)) { criterion = Restrictions.gt(propertyName, propertyValue[0]); } else if (MatchType.NE.equals(matchType)) { criterion = Restrictions.ne(propertyName, propertyValue[0]); } else if (MatchType.ISNULL.equals(matchType)) { criterion = Restrictions.isNull(propertyName); } else if (MatchType.ISNOTNULL.equals(matchType)) { criterion = Restrictions.isNotNull(propertyName); } else if (MatchType.ISEMPTY.equals(matchType)) { criterion = Restrictions.isEmpty(propertyName); } else if (MatchType.ISNOTEMPTY.equals(matchType)) { criterion = Restrictions.isNotEmpty(propertyName); } else if (MatchType.IN.equals(matchType)) { criterion = Restrictions.in(propertyName, propertyValue); } } catch (Exception e) { throw ReflectionUtils.convertReflectionExceptionToUnchecked(e); } return criterion; }
From source file:com.algoTrader.CriteriaSearch.java
/** * Adds an <code>Restrictions</code> to a <code>Criteria</code>. * * @param criteria// ww w.j a v a 2 s . co m * @param parameterName * @param parameterValue * @param comparator * @param matchMode */ private void addExpression(Criteria criteria, String parameterName, Object parameterValue, int comparator, MatchMode matchMode) { switch (comparator) { case SearchParameter.NOT_NULL_COMPARATOR: { criteria.add(Restrictions.isNotNull(parameterName)); break; } case SearchParameter.NULL_COMPARATOR: { criteria.add(Restrictions.isNull(parameterName)); break; } case SearchParameter.EMPTY_COMPARATOR: { criteria.add(Restrictions.isEmpty(parameterName)); break; } case SearchParameter.NOT_EMPTY_COMPARATOR: { criteria.add(Restrictions.isNotEmpty(parameterName)); break; } default: { if (parameterValue != null) { switch (comparator) { case SearchParameter.LIKE_COMPARATOR: { if ((matchMode != null) && (parameterValue instanceof String)) { criteria.add(Restrictions.like(parameterName, (String) parameterValue, matchMode)); } else { criteria.add(Restrictions.like(parameterName, parameterValue)); } break; } case SearchParameter.NOT_LIKE_COMPARATOR: { SimpleExpression expression; if ((matchMode != null) && (parameterValue instanceof String)) { expression = Restrictions.like(parameterName, (String) parameterValue, matchMode); } else { expression = Restrictions.like(parameterName, parameterValue); } criteria.add(Restrictions.not(expression)); break; } case SearchParameter.INSENSITIVE_LIKE_COMPARATOR: { if ((matchMode != null) && (parameterValue instanceof String)) { criteria.add(Restrictions.ilike(parameterName, (String) parameterValue, matchMode)); } else { criteria.add(Restrictions.ilike(parameterName, parameterValue)); } break; } case SearchParameter.NOT_INSENSITIVE_LIKE_COMPARATOR: { Criterion criterion; if ((matchMode != null) && (parameterValue instanceof String)) { criterion = Restrictions.ilike(parameterName, (String) parameterValue, matchMode); } else { criterion = Restrictions.ilike(parameterName, parameterValue); } criteria.add(Restrictions.not(criterion)); break; } case SearchParameter.EQUAL_COMPARATOR: { criteria.add(Restrictions.eq(parameterName, parameterValue)); break; } case SearchParameter.GREATER_THAN_OR_EQUAL_COMPARATOR: { criteria.add(Restrictions.ge(parameterName, parameterValue)); break; } case SearchParameter.GREATER_THAN_COMPARATOR: { criteria.add(Restrictions.gt(parameterName, parameterValue)); break; } case SearchParameter.LESS_THAN_OR_EQUAL_COMPARATOR: { criteria.add(Restrictions.le(parameterName, parameterValue)); break; } case SearchParameter.LESS_THAN_COMPARATOR: { criteria.add(Restrictions.lt(parameterName, parameterValue)); break; } case SearchParameter.IN_COMPARATOR: { if (parameterValue instanceof Collection) { criteria.add(Restrictions.in(parameterName, (Collection) parameterValue)); } break; } case SearchParameter.NOT_IN_COMPARATOR: { if (parameterValue instanceof Collection) { criteria.add(Restrictions.not(Restrictions.in(parameterName, (Collection) parameterValue))); } break; } case SearchParameter.NOT_EQUAL_COMPARATOR: { criteria.add(Restrictions.ne(parameterName, parameterValue)); break; } } } else { criteria.add(Restrictions.isNull(parameterName)); } } } }
From source file:com.algoTrader.CriteriaSearch.java
/** * Adds an <code>Restrictions</code> to a <code>Criteria</code>. The given <code>parameterValues</code> * represents either an array of <code>String</code> or another object. The different values in the * array are added to a disjunction or conjunction which is connected with logical and to the other criteria of the * search./*w w w .j a v a2 s . co m*/ * * @param criteria * @param parameterName * @param parameterValues * @param searchIfNull * @param comparator * @param matchMode */ private void addExpression(Criteria criteria, String parameterName, Object[] parameterValues, int comparator, MatchMode matchMode) { if (parameterValues != null) { Disjunction disjunction = null; Conjunction conjunction = null; switch (comparator) { case SearchParameter.LIKE_COMPARATOR: { disjunction = Restrictions.disjunction(); if ((matchMode != null) && (parameterValues instanceof String[])) { String[] stringParameterValues = (String[]) parameterValues; for (int index = 0; index < parameterValues.length; index++) { if (stringParameterValues[index] != null) { disjunction .add(Restrictions.like(parameterName, stringParameterValues[index], matchMode)); } else { disjunction.add(Restrictions.isNull(parameterName)); } } } else { for (int index = 0; index < parameterValues.length; index++) { if (parameterValues[index] != null) { disjunction.add(Restrictions.like(parameterName, parameterValues[index])); } else { disjunction.add(Restrictions.isNull(parameterName)); } } } break; } case SearchParameter.INSENSITIVE_LIKE_COMPARATOR: { disjunction = Restrictions.disjunction(); if ((matchMode != null) && (parameterValues instanceof String[])) { String[] stringParameterValues = (String[]) parameterValues; for (int index = 0; index < parameterValues.length; index++) { if (stringParameterValues[index] != null) { disjunction.add( Restrictions.ilike(parameterName, stringParameterValues[index], matchMode)); } else { disjunction.add(Restrictions.isNull(parameterName)); } } } else { for (int index = 0; index < parameterValues.length; index++) { if (parameterValues[index] != null) { disjunction.add(Restrictions.ilike(parameterName, parameterValues[index])); } else { disjunction.add(Restrictions.isNull(parameterName)); } } } break; } case SearchParameter.EQUAL_COMPARATOR: { disjunction = Restrictions.disjunction(); for (int index = 0; index < parameterValues.length; index++) { if (parameterValues[index] != null) { disjunction.add(Restrictions.eq(parameterName, parameterValues[index])); } else { disjunction.add(Restrictions.isNull(parameterName)); } } break; } case SearchParameter.GREATER_THAN_OR_EQUAL_COMPARATOR: { disjunction = Restrictions.disjunction(); for (int index = 0; index < parameterValues.length; index++) { if (parameterValues[index] != null) { disjunction.add(Restrictions.ge(parameterName, parameterValues[index])); } else { disjunction.add(Restrictions.isNull(parameterName)); } } break; } case SearchParameter.GREATER_THAN_COMPARATOR: { disjunction = Restrictions.disjunction(); for (int index = 0; index < parameterValues.length; index++) { if (parameterValues[index] != null) { disjunction.add(Restrictions.gt(parameterName, parameterValues[index])); } else { disjunction.add(Restrictions.isNull(parameterName)); } } break; } case SearchParameter.LESS_THAN_OR_EQUAL_COMPARATOR: { disjunction = Restrictions.disjunction(); for (int index = 0; index < parameterValues.length; index++) { if (parameterValues[index] != null) { disjunction.add(Restrictions.le(parameterName, parameterValues[index])); } else { disjunction.add(Restrictions.isNull(parameterName)); } } break; } case SearchParameter.LESS_THAN_COMPARATOR: { disjunction = Restrictions.disjunction(); for (int index = 0; index < parameterValues.length; index++) { if (parameterValues[index] != null) { disjunction.add(Restrictions.lt(parameterName, parameterValues[index])); } else { disjunction.add(Restrictions.isNull(parameterName)); } } break; } case SearchParameter.IN_COMPARATOR: { criteria.add(Restrictions.in(parameterName, parameterValues)); break; } case SearchParameter.NOT_IN_COMPARATOR: { criteria.add(Restrictions.not(Restrictions.in(parameterName, parameterValues))); break; } case SearchParameter.NOT_EQUAL_COMPARATOR: { conjunction = Restrictions.conjunction(); for (int index = 0; index < parameterValues.length; index++) { if (parameterValues[index] != null) { conjunction.add(Restrictions.ne(parameterName, parameterValues[index])); } else { conjunction.add(Restrictions.isNotNull(parameterName)); } } break; } } if (disjunction != null) { criteria.add(disjunction); } if (conjunction != null) { criteria.add(conjunction); } } else { switch (comparator) { case SearchParameter.EMPTY_COMPARATOR: { criteria.add(Restrictions.isEmpty(parameterName)); break; } case SearchParameter.NOT_EMPTY_COMPARATOR: { criteria.add(Restrictions.isNotEmpty(parameterName)); break; } default: { criteria.add(Restrictions.isNull(parameterName)); } } } }
From source file:com.blue.ssh.core.orm.hibernate.HibernateDao.java
License:Apache License
/** * ??Criterion,.//from w w w . j ava2 s .c om */ protected Criterion buildCriterion(final String propertyName, final Object propertyValue, final MatchType matchType) { Assert.hasText(propertyName, "propertyName?"); Criterion criterion = null; // ?MatchTypecriterion switch (matchType) { case EQ: criterion = Restrictions.eq(propertyName, propertyValue); break; case LIKE: criterion = Restrictions.like(propertyName, (String) propertyValue, MatchMode.ANYWHERE); break; case LE: criterion = Restrictions.le(propertyName, propertyValue); break; case LT: criterion = Restrictions.lt(propertyName, propertyValue); break; case GE: criterion = Restrictions.ge(propertyName, propertyValue); break; case GT: criterion = Restrictions.gt(propertyName, propertyValue); case NE: criterion = Restrictions.ne(propertyName, propertyValue); } return criterion; }
From source file:com.bookshop.dao.BookDAOImpl.java
@Override public List<Book> listBooks(String bookNameCriteria, String isbnCriteria) { if (bookNameCriteria == null && isbnCriteria == null) { return new ArrayList<Book>(); }/* w ww .j av a 2s .c om*/ /* Query query; if (isbnCriteria == null) { query = session.createQuery("FROM Books WHERE BookName LIKE '%" + bookNameCriteria + "%'"); } else if(bookNameCriteria == null) { query = session.createQuery("FROM Books WHERE ISBN LIKE '%" + isbnCriteria + "%'"); } else // if(bookNameCriteria != null && isbnCriteria != null) { query = session.createQuery("FROM Books WHERE ISBN LIKE '%" + isbnCriteria + "%' AND BookName LIKE '%" + bookNameCriteria + "%'"); } return query.list(); */ Criteria criteria = session.createCriteria(Book.class); if (isbnCriteria == null) { Criterion bookNameCriterion = Restrictions.like("bookName", bookNameCriteria, MatchMode.ANYWHERE); criteria.add(bookNameCriterion); } else if (bookNameCriteria == null) { Criterion isbnCriterion = Restrictions.like("ISBN", isbnCriteria, MatchMode.ANYWHERE); criteria.add(isbnCriterion); } else // if(bookNameCriteria != null && isbnCriteria != null) { Criterion bookNameCriterion = Restrictions.like("bookName", bookNameCriteria, MatchMode.ANYWHERE); Criterion isbnCriterion = Restrictions.like("ISBN", isbnCriteria, MatchMode.ANYWHERE); LogicalExpression orExp = Restrictions.and(bookNameCriterion, isbnCriterion); criteria.add(orExp); } return criteria.list(); }
From source file:com.bookshop.dao.BookDAOImpl.java
@Override public List<Book> listBooks(String searchCriteria) { if (searchCriteria == null) { return new ArrayList<Book>(); }/*from w ww . j a va 2 s . c om*/ Criteria criteria = session.createCriteria(Book.class); Criterion bookNameCriterion = Restrictions.like("bookName", searchCriteria, MatchMode.ANYWHERE); Criterion isbnCriterion = Restrictions.like("ISBN", searchCriteria, MatchMode.ANYWHERE); Criterion editionCriterion = Restrictions.like("edition", searchCriteria, MatchMode.ANYWHERE); // Criterion yearCriterion = Restrictions.eq("year", searchCriteria); Criterion synopsisCriterion = Restrictions.like("synopsis", searchCriteria, MatchMode.ANYWHERE); Criterion aboutAuthorsCriterion = Restrictions.like("aboutAuthors", searchCriteria, MatchMode.ANYWHERE); Criterion topicsCoveredCriterion = Restrictions.like("topicsCovered", searchCriteria, MatchMode.ANYWHERE); Criterion contentsCDROMCriterion = Restrictions.like("contentsCDROM", searchCriteria, MatchMode.ANYWHERE); // Criterion costCriterion = Restrictions.like("cost", searchCriteria, MatchMode.ANYWHERE); Criterion firstAuthorCriterion = Restrictions.like("firstAuthor", searchCriteria, MatchMode.ANYWHERE); Criterion secondAuthorCriterion = Restrictions.like("secondAuthor", searchCriteria, MatchMode.ANYWHERE); Criterion thirdAuthorCriterion = Restrictions.like("thirdAuthor", searchCriteria, MatchMode.ANYWHERE); Criterion fourthAuthorCriterion = Restrictions.like("fourthAuthor", searchCriteria, MatchMode.ANYWHERE); criteria.add(Restrictions.disjunction().add(bookNameCriterion).add(isbnCriterion).add(editionCriterion) .add(synopsisCriterion).add(aboutAuthorsCriterion).add(topicsCoveredCriterion) .add(contentsCDROMCriterion).add(firstAuthorCriterion).add(secondAuthorCriterion) .add(thirdAuthorCriterion).add(fourthAuthorCriterion)); return criteria.list(); }
From source file:com.cai310.lottery.web.controller.lottery.keno.KenoController.java
/** * //from www. j a va 2s . c o m * * @return * @throws WebDataException */ public String list() throws WebDataException { User user = getLoginUser(); if (user == null) { addActionError(",???."); return this.error(); } Class<S> cls = kenoService.getSchemeClass(); XDetachedCriteria criteria = new XDetachedCriteria(cls); if (scheme != null && !StringUtils.isBlank(scheme.getPeriodNumber())) { criteria.add(Restrictions.like("periodNumber", scheme.getPeriodNumber(), MatchMode.START)); } criteria.add(Restrictions.eq("sponsorId", user.getId())); criteria.setMaxResults(count); criteria.addOrder(Order.desc("id")); pagination = kenoService.findByCriteriaAndPagination(criteria, pagination); this.loadNewsList(); return "list"; }
From source file:com.cai310.lottery.web.controller.lottery.keno.KenoController.java
/** * ?//from w w w. j a va 2s .co m * * @return */ public String note() { Class<I> cls = kenoService.getIssueDataClass(); XDetachedCriteria criteria = new XDetachedCriteria(cls); if (period != null && !StringUtils.isBlank(period.getPeriodNumber())) { criteria.add(Restrictions.like("periodNumber", period.getPeriodNumber(), MatchMode.START)); } criteria.add(Restrictions.gt("state", IssueState.ISSUE_SATE_RESULT)); criteria.add(Restrictions.isNotNull("results")); // criteria.setMaxResults(count); criteria.addOrder(Order.desc("id")); // zhuhui motify by 2011-05-03 20 30 50 pagesize 20 30 50 // ?? if (count > 0) { pagination.setPageSize(count); } pagination = kenoService.findByCriteriaAndPagination(criteria, pagination); loadNewsList(); return "note"; }