Example usage for org.hibernate.criterion Restrictions disjunction

List of usage examples for org.hibernate.criterion Restrictions disjunction

Introduction

In this page you can find the example usage for org.hibernate.criterion Restrictions disjunction.

Prototype

public static Disjunction disjunction() 

Source Link

Document

Group expressions together in a single disjunction (A or B or C...).

Usage

From source file:info.interactivesystems.spade.dao.NilsimsaSimilarityDao.java

License:Apache License

@SuppressWarnings("unchecked")
public NilsimsaSimilarity findSimilarityByReviewId(String reviewId) {
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(NilsimsaSimilarity.class);

    criteria.add(Restrictions.disjunction().add(Restrictions.eq("reviewA.id", reviewId))
            .add(Restrictions.eq("reviewB.id", reviewId)));
    criteria.addOrder(Order.desc("similarity"));

    List<NilsimsaSimilarity> resultList = criteria.list();

    if (!resultList.isEmpty()) {
        return resultList.get(0);
    }//from  w  w  w . ja  va  2  s  .  c o  m

    return null;
}

From source file:info.interactivesystems.spade.dao.NilsimsaSimilarityDao.java

License:Apache License

@SuppressWarnings("unchecked")
public List<NilsimsaSimilarity> findSimilaritiesByReviewId(String reviewId) {
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(NilsimsaSimilarity.class);

    criteria.add(Restrictions.disjunction().add(Restrictions.eq("reviewA.id", reviewId))
            .add(Restrictions.eq("reviewB.id", reviewId)));
    criteria.addOrder(Order.desc("similarity"));

    List<NilsimsaSimilarity> resultList = criteria.list();

    if (!resultList.isEmpty()) {
        return resultList;
    }/*from  w w w .  j av a 2s. co m*/

    return null;
}

From source file:io.heming.accountbook.facade.RecordQuerier.java

License:Apache License

/**
 * ??????Criteria.//w  ww.  j a v  a2  s  .  c o m
 *
 * @param criteria
 */
private void buildCriteria(Criteria criteria) {
    // 
    if (startDate != null) {
        criteria.add(Restrictions.ge("date", startDate));
    }
    if (endDate != null) {
        criteria.add(Restrictions.le("date", endDate));
    }

    // ?
    if (keyword != null && keyword.length() > 0) {
        criteria.createAlias("category", "c");
        for (String item : keyword.split("\\s")) {
            Junction or = Restrictions.disjunction();
            // ???
            or.add(Restrictions.like("c.name", item, MatchMode.ANYWHERE));
            // ?\d\d\.\d????
            if (Pattern.matches("\\d+\\.\\d+", item)) {
                or.add(Restrictions.eq("price", Double.parseDouble(item)));
            }
            // ???3????
            if (Pattern.matches("\\d{3,11}", item)) {
                or.add(Restrictions.like("phone", item, MatchMode.ANYWHERE));
            }
            // ?
            or.add(Restrictions.like("note", item, MatchMode.ANYWHERE));
            criteria.add(or);
        }
    }

    // 
    criteria.setFirstResult(firstIndex).setMaxResults(maxSize);

    // ?
    if (orderBy != null) {
        Order order = desc ? Order.desc(orderBy) : Order.asc(orderBy);
        criteria.addOrder(order);
    }
}

From source file:it.av.eatt.service.impl.EaterRelationServiceHibernate.java

License:Apache License

/**
 * {@inheritDoc}/*  ww  w . j a  v  a2 s  .c om*/
 */
@Override
public List<EaterRelation> getAllRelations(Eater ofUser) {
    Conjunction pendingFriend = Restrictions.conjunction();
    pendingFriend.add(Restrictions.eq(EaterRelation.STATUS, EaterRelation.STATUS_PENDING));
    pendingFriend.add(Restrictions.eq(EaterRelation.TYPE, EaterRelation.TYPE_FRIEND));
    pendingFriend.add(Restrictions.eq(EaterRelation.TO_USER, ofUser));
    Disjunction inOr = Restrictions.disjunction();
    inOr.add(pendingFriend);
    inOr.add(Restrictions.eq(EaterRelation.FROM_USER, ofUser));
    return findByCriteria(inOr);
}

From source file:it.av.youeat.service.impl.CommentServiceHibernate.java

License:Apache License

/**
 * {@inheritDoc}/* w w  w.j  a  va2s  .  c o  m*/
 */
@Override
public List<Comment> find(String pattern, int first, int maxResults, String sortField, boolean isAscending) {
    Criterion critByTitle = null;
    Criterion critByBody = null;
    //Criterion critByLastname = null;
    //Criterion critByFirstname = null;
    if (StringUtils.isNotBlank(pattern)) {
        //critByFirstname = Restrictions.ilike(Comment.AUTHOR_FIELD + ".firstname", pattern);
        //critByLastname = Restrictions.ilike(Comment.AUTHOR_FIELD + ".lastname", pattern);
        critByTitle = Restrictions.ilike(Comment.TITLE_FIELD, pattern);
        critByBody = Restrictions.ilike(Comment.BODY_FIELD, pattern);
    }

    Disjunction critInOr = Restrictions.disjunction();
    //critInOr.add(critByFirstname);
    //critInOr.add(critByLastname);
    critInOr.add(critByBody);
    critInOr.add(critByTitle);

    Order order = null;
    if (isAscending) {
        order = Order.asc(sortField);
    } else {
        order = Order.desc(sortField);
    }
    return findByCriteria(order, first, maxResults, critInOr);
}

From source file:it.av.youeat.service.impl.DialogServiceHibernate.java

License:Apache License

/**
 * {@inheritDoc}//  www. ja  va2s .  c o m
 */
@Override
public List<Dialog> getDialogs(Eater eater, boolean excludeSingleMessage) {
    Conjunction receiverFilter = Restrictions.conjunction();
    Criterion critByReceiver1 = Restrictions.eq(Dialog.RECEIVER_FIELD, eater);
    Criterion critByReceiver2 = Restrictions.eq(Dialog.DELETED_FROM_RECEIVER_FIELD, false);
    receiverFilter.add(critByReceiver1);
    receiverFilter.add(critByReceiver2);

    Conjunction senderFilter = Restrictions.conjunction();
    Criterion critBySender1 = Restrictions.eq(Dialog.SENDER_FIELD, eater);
    Criterion critBySender2 = Restrictions.eq(Dialog.DELETED_FROM_SENDER_FIELD, false);
    senderFilter.add(critBySender1);
    senderFilter.add(critBySender2);

    Disjunction or = Restrictions.disjunction();
    or.add(receiverFilter);
    or.add(senderFilter);
    Order orderBYDate = Order.desc(Dialog.CREATION_TIME_FIELD);
    List<Dialog> results = findByCriteria(orderBYDate, or);
    if (excludeSingleMessage) {
        for (int i = 0; i < results.size(); i++) {
            if (results.get(i).getSender().equals(eater) && results.get(i).getMessages().size() == 1) {
                results.remove(i);
            }
        }
    }
    return results;
}

From source file:it.av.youeat.service.impl.EaterRelationServiceHibernate.java

License:Apache License

/**
 * {@inheritDoc}//from w ww  .j a va2 s .c  om
 */
@Override
public List<EaterRelation> getAllRelations(Eater ofUser) {
    Conjunction pendingFriend = Restrictions.conjunction();
    pendingFriend.add(Restrictions.eq(EaterRelation.STATUS, EaterRelation.STATUS_PENDING));
    pendingFriend.add(Restrictions.eq(EaterRelation.TYPE, EaterRelation.TYPE_FRIEND));
    pendingFriend.add(Restrictions.eq(EaterRelation.TO_USER, ofUser));
    Disjunction inOr = Restrictions.disjunction();
    inOr.add(pendingFriend);
    inOr.add(Restrictions.eq(EaterRelation.FROM_USER, ofUser));
    List<EaterRelation> results = findByCriteria(inOr);
    Collections.sort(results);
    return results;
}

From source file:it.av.youeat.service.impl.EaterRelationServiceHibernate.java

License:Apache License

/**
 * Return all the relations on the given user without any filter.
 *///  www . j a  va2 s  . c om
private Collection<EaterRelation> getRelations(Eater ofUser) {
    Conjunction pendingFriend = Restrictions.conjunction();
    pendingFriend.add(Restrictions.eq(EaterRelation.TO_USER, ofUser));
    Disjunction inOr = Restrictions.disjunction();
    inOr.add(pendingFriend);
    inOr.add(Restrictions.eq(EaterRelation.FROM_USER, ofUser));
    List<EaterRelation> results = findByCriteria(inOr);
    return results;
}

From source file:it.cilea.osd.common.dao.impl.ApplicationDao.java

License:Open Source License

/**
 * {@inheritDoc}/* w  w  w.jav  a 2  s.com*/
 */
public <T, PK extends Serializable> List<T> getList(Class<T> clazz, List<PK> allIds) {
    Session session = getSession();
    Criteria criteria = session.createCriteria(clazz);

    final int maxResults = allIds.size();
    if (maxResults == 0)
        return null;

    int loop = maxResults / MAX_IN_CLAUSE;
    boolean exact = maxResults % MAX_IN_CLAUSE == 0;

    if (!exact)
        loop++;

    Disjunction disjunction = Restrictions.disjunction();

    for (int index = 0; index < loop; index++) {
        int max = index * MAX_IN_CLAUSE + MAX_IN_CLAUSE <= maxResults ? index * MAX_IN_CLAUSE + MAX_IN_CLAUSE
                : maxResults;
        List<PK> ids = new ArrayList<PK>(max - index * MAX_IN_CLAUSE);
        for (int entityInfoIndex = index * MAX_IN_CLAUSE; entityInfoIndex < max; entityInfoIndex++) {
            ids.add(allIds.get(entityInfoIndex));
        }
        disjunction.add(Restrictions.in("id", ids));
    }
    criteria.add(disjunction);
    criteria.list(); // load all objects

    // mandatory to keep the same ordering
    List<T> result = new ArrayList<T>(allIds.size());
    for (PK id : allIds) {
        T element = (T) session.load(clazz, id);
        if (Hibernate.isInitialized(element)) {
            // all existing elements should have been loaded by the query,
            // the other ones are missing ones
            result.add(element);
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Object with id: " + id + " not in database: " + clazz);
            }
        }
    }
    return result;
}

From source file:jp.ac.tokushima_u.is.ll.common.orm.hibernate.HibernateDao.java

License:Apache License

/**
 * ??Criterion[]??./*from   ww w.j  av  a2 s.  c o m*/
 */
protected Criterion[] buildPropertyFilterCriterions(final List<PropertyFilter> filters) {
    List<Criterion> criterionList = new ArrayList<Criterion>();
    for (PropertyFilter filter : filters) {
        if (!filter.isMultiProperty()) { //?????????.
            Criterion criterion = buildPropertyFilterCriterion(filter.getPropertyName(),
                    filter.getPropertyValue(), filter.getMatchType());
            criterionList.add(criterion);
        } else {//?????????or??.
            Disjunction disjunction = Restrictions.disjunction();
            for (String param : filter.getPropertyNames()) {
                Criterion criterion = buildPropertyFilterCriterion(param, filter.getPropertyValue(),
                        filter.getMatchType());
                disjunction.add(criterion);
            }
            criterionList.add(disjunction);
        }
    }
    return criterionList.toArray(new Criterion[criterionList.size()]);
}