Example usage for org.hibernate.criterion Restrictions in

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

Introduction

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

Prototype

public static Criterion in(String propertyName, Collection values) 

Source Link

Document

Apply an "in" constraint to the named property.

Usage

From source file:com.photon.phresco.eshop.service.EShopService.java

License:Apache License

public List<ProductHBM> getSpecialProducts() throws EShopException {
    @SuppressWarnings("unchecked")
    List<ProductHBM> productList = (List<ProductHBM>) template.execute(new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            DetachedCriteria criteria = DetachedCriteria.forClass(ProductHBM.class)
                    .add(Restrictions.eq("specialProduct", 1));

            List<ProductHBM> productHBMs = template.findByCriteria(criteria);
            List<ProductHBM> productHBMList = new ArrayList<ProductHBM>(10);

            for (ProductHBM productHBM : productHBMs) {
                Object[] values = { 1, 2, 3, 4, 5 };
                List<ReviewHBM> reviewHBMs = session.createCriteria(ReviewHBM.class)
                        .add(Restrictions.in("ratings", values))
                        .add(Restrictions.eq("productId", productHBM.getProductId())).list();

                int rating = 0;

                // TODO average rating calculation not working
                if (reviewHBMs != null && reviewHBMs.size() > 0) {
                    ReviewHBM reviewHBM = reviewHBMs.get(0);
                    rating = ServiceUtil.getRating(reviewHBM.getRatings());
                }/*  w w  w. ja v  a  2 s . c  o  m*/

                productHBM.setRating(rating);
                productHBMList.add(productHBM);
            }

            return productHBMList;
        }
    });

    return productList;
}

From source file:com.photon.phresco.eshop.service.EShopService.java

License:Apache License

public List<ProductHBM> getNewProducts() throws EShopException {
    @SuppressWarnings("unchecked")
    List<ProductHBM> productList = (List<ProductHBM>) template.execute(new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            DetachedCriteria criteria = DetachedCriteria.forClass(ProductHBM.class)
                    .add(Restrictions.eq("newProduct", 1));

            List<ProductHBM> productHBMs = template.findByCriteria(criteria);
            List<ProductHBM> productHBMList = new ArrayList<ProductHBM>(10);

            for (ProductHBM productHBM : productHBMs) {
                Object[] values = { 1, 2, 3, 4, 5 };
                List<ReviewHBM> reviewHBMs = session.createCriteria(ReviewHBM.class)
                        .add(Restrictions.in("ratings", values))
                        .add(Restrictions.eq("productId", productHBM.getProductId())).list();

                int rating = 0;

                // TODO average rating calculation not working
                if (reviewHBMs != null && reviewHBMs.size() > 0) {
                    ReviewHBM reviewHBM = reviewHBMs.get(0);
                    rating = ServiceUtil.getRating(reviewHBM.getRatings());
                }/*  w w  w . j  a v  a  2s.  com*/

                productHBM.setRating(rating);
                productHBMList.add(productHBM);
            }

            return productHBMList;
        }
    });

    return productList;
}

From source file:com.photon.phresco.eshop.service.EShopService.java

License:Apache License

public ProductHBM getProduct(final int productId) throws EShopException {
    @SuppressWarnings("unchecked")
    ProductHBM productHBM = (ProductHBM) template.execute(new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            DetachedCriteria criteria = DetachedCriteria.forClass(ProductHBM.class)
                    .add(Restrictions.eq("productId", productId));

            List<ProductHBM> productHBMs = template.findByCriteria(criteria);
            ProductHBM productHBM = null;

            if (productHBMs.size() > 0) {
                productHBM = productHBMs.get(0);
                Object[] values = { 1, 2, 3, 4, 5 };

                List<ReviewHBM> reviewHBMs = session.createCriteria(ReviewHBM.class)
                        .add(Restrictions.in("ratings", values))
                        .add(Restrictions.eq("productId", productHBM.getProductId())).list();

                int rating = 0;

                // TODO average rating calculation not working
                if (reviewHBMs != null && reviewHBMs.size() > 0) {
                    ReviewHBM reviewHBM = reviewHBMs.get(0);
                    rating = ServiceUtil.getRating(reviewHBM.getRatings());
                }//w ww  .  j a  v a2s .c  o m

                productHBM.setRating(rating);
            }

            return productHBM;
        }
    });

    return productHBM;
}

From source file:com.photon.phresco.eshop.service.EShopService.java

License:Apache License

public List<ProductHBM> searchProducts(final String namePattern) throws EShopException {
    @SuppressWarnings("unchecked")
    List<ProductHBM> productList = (List<ProductHBM>) template.execute(new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            DetachedCriteria criteria = DetachedCriteria.forClass(ProductHBM.class)
                    .add(Restrictions.like("productName", "%" + namePattern + "%"))
                    .add(Restrictions.like("productDescription", "%" + namePattern + "%"));

            List<ProductHBM> productHBMs = template.findByCriteria(criteria);
            List<ProductHBM> productHBMList = new ArrayList<ProductHBM>(10);

            for (ProductHBM productHBM : productHBMs) {
                Object[] values = { 1, 2, 3, 4, 5 };
                List<ReviewHBM> reviewHBMs = session.createCriteria(ReviewHBM.class)
                        .add(Restrictions.in("ratings", values))
                        .add(Restrictions.eq("productId", productHBM.getProductId())).list();

                int rating = 0;

                // TODO average rating calculation not working
                if (reviewHBMs != null && reviewHBMs.size() > 0) {
                    ReviewHBM reviewHBM = reviewHBMs.get(0);
                    rating = ServiceUtil.getRating(reviewHBM.getRatings());
                }/*from  w  w w .j a v  a 2  s.  c o m*/

                productHBM.setRating(rating);
                productHBMList.add(productHBM);
            }

            return productHBMList;
        }
    });

    return productList;
}

From source file:com.pmo.pmoitserv.Dao.ActionDao.java

public long getEnCoursActionCount_byCompte(int compteId) {
    Long result = 0L;/*from  www  .ja v  a  2s  . co  m*/
    ;
    Compte cpte = null;
    List<Projet> pjts = new ArrayList<Projet>();
    List<Sousprojet> souspjts = new ArrayList<Sousprojet>();
    List<Chantier> chantiers = new ArrayList<Chantier>();
    try {
        this.session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        cpte = (Compte) session.get(Compte.class, compteId);
        pjts = session.createCriteria(Projet.class).add(Restrictions.eq("compte", cpte)).list();
        souspjts = session.createCriteria(Sousprojet.class).add(Restrictions.in("projet", pjts)).list();
        chantiers = session.createCriteria(Chantier.class).add(Restrictions.in("sousprojet", souspjts)).list();
        Criteria cr = session.createCriteria(Action.class);
        Criterion c1 = Restrictions.ilike("actionStatut", "cours", MatchMode.ANYWHERE);
        Criterion c2 = Restrictions.in("chantier", chantiers);
        cr.add(Restrictions.and(c1, c2));
        cr.setProjection(Projections.rowCount());
        result = (Long) cr.uniqueResult();
        tx.commit();
        session.flush();
        session.close();
        return result;

    } catch (Exception e) {
        e.printStackTrace();
        session.close();
    }
    return result;
}

From source file:com.ponysdk.hibernate.query.decorator.AbstractCriteriaDecorator.java

License:Apache License

@SuppressWarnings("rawtypes")
@Override/*w ww. j a va 2  s  . com*/
public void render(final CriteriaContext context) {
    final Criterion field = context.getCriterion();
    Criteria criteria = context.getOrderingCriteria();

    final List<String> propertyNamePath = Arrays.asList(field.getPojoProperty().split(REGEX_SPLIT));
    final Iterator<String> iter = propertyNamePath.iterator();
    String key = null;
    String associationPath = null;
    if (propertyNamePath.size() == 1) {
        associationPath = iter.next();
    } else
        while (iter.hasNext()) {
            key = iter.next();
            if (associationPath == null) {
                associationPath = new String(key);
            } else {
                associationPath += "." + key;
            }
            if (iter.hasNext()) {
                criteria = criteria.createCriteria(associationPath, key, CriteriaSpecification.INNER_JOIN);
                associationPath = new String(key);
            }
        }

    final T value = getObjectValue(field);
    ComparatorType comparator = field.getComparator();

    if (value != null) {
        if (value.toString().contains("%")) {
            comparator = ComparatorType.LIKE;
        }
    }

    if (field.getValue() != null || field.getComparator() == ComparatorType.IS_NULL
            || field.getComparator() == ComparatorType.IS_NOT_NULL) {

        switch (comparator) {
        case EQ:
            criteria.add(Restrictions.eq(associationPath, value));
            break;
        case GE:
            criteria.add(Restrictions.ge(associationPath, value));
            break;
        case GT:
            criteria.add(Restrictions.gt(associationPath, value));
            break;
        case LE:
            criteria.add(Restrictions.le(associationPath, value));
            break;
        case LT:
            criteria.add(Restrictions.lt(associationPath, value));
            break;
        case NE:
            criteria.add(Restrictions.ne(associationPath, value));
            break;
        case LIKE:
            criteria.add(Restrictions.ilike(associationPath, value));
            break;
        case IS_NULL:
            criteria.add(Restrictions.isNull(associationPath));
            break;
        case IS_NOT_NULL:
            criteria.add(Restrictions.isNotNull(associationPath));
            break;
        case IN:
            if (value instanceof Collection) {
                criteria.add(Restrictions.in(associationPath, (Collection) value));
            } else if (value instanceof Object[]) {
                criteria.add(Restrictions.in(associationPath, (Object[]) value));
            } else {
                log.warn("Type not allowed for IN clause: " + value.getClass() + ", value: " + value);
            }
            break;

        default:
            log.warn("Restriction not supported: " + comparator);
            break;
        }
    }

    switch (field.getSortingType()) {
    case ASCENDING:
        criteria.addOrder(Order.asc(associationPath));
        break;
    case DESCENDING:
        criteria.addOrder(Order.desc(associationPath));
        break;
    case NONE:
        break;
    }

}

From source file:com.qcadoo.model.api.search.SearchRestrictions.java

License:Open Source License

/**
 * Creates criterion which checks if field is in given values.
 * /*from  w  ww . j ava2  s  .com*/
 * @param field
 *            field
 * @param values
 *            values
 * @return criterion
 */
public static SearchCriterion in(final String field, final Collection<?> values) {
    return new SearchCriterionImpl(Restrictions.in(field, values));
}

From source file:com.qcadoo.model.api.search.SearchRestrictions.java

License:Open Source License

/**
 * Creates criterion which checks if field is in given values.
 * //from   w w w . ja v a  2  s  . c  om
 * @param field
 *            field
 * @param values
 *            values
 * @return criterion
 */
public static SearchCriterion in(final String field, final Object... values) {
    return new SearchCriterionImpl(Restrictions.in(field, values));
}

From source file:com.registryKit.program.programDAO.java

/**
 * The 'getProgramsByAdministratory' function will return a list of programs associated to the program admin
 * logging in.//from w  w  w . j a v a2 s  . c  om
 * 
 * @param userId    The id of the program admin.
 * @return
 * @throws Exception 
 */
public List<program> getProgramsByAdminisrator(Integer userId) throws Exception {

    Query query = sessionFactory.getCurrentSession()
            .createQuery("from programAdmin where systemUserId = :systemUserId");
    query.setParameter("systemUserId", userId);

    List<programAdmin> adminProgramList = query.list();
    List<Integer> programIds = null;

    if (!adminProgramList.isEmpty()) {
        programIds = new CopyOnWriteArrayList<Integer>();

        for (programAdmin admin : adminProgramList) {
            programIds.add(admin.getProgramId());
        }
    }

    List<program> programs = null;

    if (!programIds.isEmpty()) {

        Criteria criteria = sessionFactory.getCurrentSession().createCriteria(program.class);
        criteria.add(Restrictions.in("id", programIds));

        programs = criteria.list();

    }

    return programs;

}

From source file:com.reignite.parser.Join.java

License:Open Source License

/**
 * Prepares the criteria for execution by adding an "in" clause using the
 * given ids and by setting the order for more efficient processing.
 * //from   w w w.ja va  2  s.c  om
 * @param ids
 */
public void prepare(Set<Object> ids) {
    this.criteria.add(Restrictions.in(joinId, ids));
    if (this.orders == null || this.orders.isEmpty()) {
        this.criteria.addOrder(Order.asc(joinId));
    } else {
        for (Order order : orders) {
            this.criteria.addOrder(order);
        }
    }
}