Example usage for org.hibernate Criteria createCriteria

List of usage examples for org.hibernate Criteria createCriteria

Introduction

In this page you can find the example usage for org.hibernate Criteria createCriteria.

Prototype

public Criteria createCriteria(String associationPath) throws HibernateException;

Source Link

Document

Create a new Criteria, "rooted" at the associated entity.

Usage

From source file:com.yahoo.elide.datastores.hibernate3.filter.CriteriaExplorer.java

License:Apache License

/**
 * Build a set of criteria from sessionCriteria.
 *
 * NOTE: It is assumed that sessionCriteria is a criteria corresponding to the "loadClass"
 *       that this was instantiated with.
 *
 * @param sessionCriteria Session criteria to update
 * @param session Session to create criteria on
 *///from  w w w. jav a  2s  . c o m
public void buildCriteria(final Criteria sessionCriteria, final Session session) {
    if (rootCriterion != null) {
        sessionCriteria.add(rootCriterion);
    }

    for (Map.Entry<String, Set<Predicate>> entry : requestScope.getPredicates().entrySet()) {
        String criteriaPath = entry.getKey();
        Set<Predicate> predicates = entry.getValue();

        String[] objects = criteriaPath.split("\\.");

        Criteria criteria;
        Class filterClass = dictionary.getBinding(objects[0]);
        if (loadClass.equals(filterClass)) {
            criteria = sessionCriteria;
        } else {
            criteria = session.createCriteria(filterClass);
        }

        for (int i = 1; i < objects.length; ++i) {
            criteria = criteria.createCriteria(objects[i]);
        }
        criteria.add(filterOperation.applyAll(predicates));
    }
}

From source file:corner.orm.tapestry.table.PersistentBasicTableModel.java

License:Apache License

/**
 *
 * @see org.apache.tapestry.contrib.table.model.IBasicTableModel#getCurrentPageRows(int, int, org.apache.tapestry.contrib.table.model.ITableColumn, boolean)
 *//*w  w w  .  j  av  a  2  s  . c  om*/
public Iterator getCurrentPageRows(final int nFirst, final int nPageSize, final ITableColumn column,
        final boolean sort) {
    if (isRewinding) {
        if (logger.isDebugEnabled()) {
            logger.debug("is rewinding ,return false;");
        }

        return null;
    }
    if (this.resultList == null) {
        resultList = this.entityService.executeFind(new HibernateCallback() {

            public Object doInHibernate(Session session) throws HibernateException, SQLException {
                Criteria criteria = callback.createCriteria(session);
                callback.appendCriteria(criteria);

                if (column != null) {

                    String columnName = column.getColumnName();
                    if (columnName.indexOf(".") > 0) {
                        Criteria newCriteria = null;
                        String[] props = columnName.split("\\.");
                        int index = props.length - 1;
                        String orderColumn = props[index];//
                        for (int i = 0; i < props.length - 1; i++) {//? 
                            if (newCriteria == null) {//newCriteria
                                newCriteria = criteria.createCriteria(props[i]);
                            } else {
                                newCriteria = newCriteria.createCriteria(props[i]);
                            }
                        }
                        if (newCriteria != null) {
                            newCriteria.addOrder(sort ? Order.desc(orderColumn) : Order.asc(orderColumn));
                        } else {
                            criteria.addOrder(sort ? Order.desc(column.getColumnName())
                                    : Order.asc(column.getColumnName()));
                        }
                    } else {
                        criteria.addOrder(
                                sort ? Order.desc(column.getColumnName()) : Order.asc(column.getColumnName()));
                    }

                }

                callback.appendOrder(criteria);//? since 2.2.1

                criteria.setFirstResult(nFirst);
                criteria.setMaxResults(nPageSize);

                return criteria.list();
            }
        });
    }
    return resultList.iterator();

}

From source file:criteria.CriteriaProduto.java

public static Criteria listarProdutos(String filter, int select) {
    Criteria criteria = Hibernate.getSession().createCriteria(Produto.class);

    if (select == FILTRO_CATEGORIA) {
        Criteria cr = criteria.createCriteria("categoria");
        cr.add(Restrictions.like("nomeCategoria", "%" + filter + "%"));
    }/*from ww w .j a  va 2  s .c om*/
    if (select == FILTRO_PRODUTO) {
        criteria.add(Restrictions.like("nomeProduto", "%" + filter + "%"));
    }

    return criteria;
}

From source file:data.dao.SequenceDao.java

public List<Sequence> findBySceneId(Scene sc) {
    Criteria cr = currentSession().createCriteria(getSupportedClass());
    cr.createCriteria("scenes").add(Restrictions.eq("sceneId", sc.getId()));
    return cr.list();
}

From source file:de.congrace.blog4j.dao.CategoryDaoImpl.java

License:Apache License

public int getNextOrderValue(Category parent) {
    Criteria crit = getCurrentSession().createCriteria(Category.class);
    if (parent == null) {
        crit.add(Restrictions.isNull("parentCategory"));
    } else {/*from  w  ww.j  a  v a  2 s  .  c o  m*/
        crit.createCriteria("parentCategory").add(Restrictions.idEq(parent.getId()));
    }
    crit.setProjection(Projections.rowCount());
    return ((Integer) crit.uniqueResult()).intValue();
}

From source file:de.congrace.blog4j.dao.CategoryDaoImpl.java

License:Apache License

public Category getCategoryByOrder(Category parent, int order) {
    Criteria crit = this.getCurrentSession().createCriteria(Category.class)
            .add(Restrictions.eq("order", order));
    if (parent == null) {
        crit.add(Restrictions.isNull("parentCategory"));
    } else {/*w w w  . jav  a  2  s  .  co  m*/
        crit.createCriteria("parentCategory").add(Restrictions.idEq(parent.getId()));
    }
    return (Category) crit.uniqueResult();
}

From source file:de.iteratec.iteraplan.persistence.dao.GenericBaseDAO.java

License:Open Source License

/** {@inheritDoc} */
@SuppressWarnings("unchecked")
public List<E> getSubscribedElements() {
    HibernateCallback<List<E>> callback = new HibernateCallback<List<E>>() {
        public List<E> doInHibernate(Session session) {
            Criteria c = session.createCriteria(getPersistentClass());
            c.createCriteria("subscribedUsers")
                    .add(Restrictions.idEq(UserContext.getCurrentUserContext().getUser().getId()));

            return c.list();
        }// ww w  .j  a va  2s .co  m
    };

    return getHibernateTemplate().executeFind(callback);
}

From source file:de.powerstaff.business.dao.hibernate.PersonDAOHibernateImpl.java

License:Open Source License

protected Collection<GenericSearchResult> performSearchByContact(final String aContact,
        final ContactType aContactType, final String[] aDisplayProperties, final String[] aOrderByProperties,
        final int aMax) {
    return (Collection<GenericSearchResult>) getHibernateTemplate().execute(new HibernateCallback() {

        public Object doInHibernate(Session aSession) throws SQLException {
            Criteria theCriteria = aSession.createCriteria(getEntityClass());

            ProjectionList theList = Projections.projectionList();
            theList.add(Projections.property("id"));
            for (String theProperty : aDisplayProperties) {
                theList.add(Projections.property(theProperty));
            }/*from w  ww  .  j  a  v  a2 s. c o  m*/

            theCriteria.setProjection(theList);

            for (String theProperty : aOrderByProperties) {
                theCriteria.addOrder(Order.asc(theProperty));
            }

            Criteria theContacts = theCriteria.createCriteria("contacts");
            theContacts.add(Restrictions.eq("type", aContactType));
            theContacts.add(Restrictions.ilike("value", "%" + aContact + "%"));

            Collection<GenericSearchResult> theResult = new ArrayList<GenericSearchResult>();

            theCriteria.setMaxResults(aMax);
            for (Iterator it = theCriteria.list().iterator(); it.hasNext();) {
                Object[] theRow = (Object[]) it.next();
                GenericSearchResult theRowObject = new GenericSearchResult();
                theRowObject.put(GenericSearchResult.OBJECT_ID_KEY, theRow[0]);
                for (int i = 0; i < aDisplayProperties.length; i++) {
                    theRowObject.put(aDisplayProperties[i], theRow[i + 1]);
                }
                theResult.add(theRowObject);
            }

            return theResult;

        }

    });
}

From source file:de.tudarmstadt.ukp.lmf.api.Uby.java

License:Apache License

/**
 * Fetches a {@link List} of {@link LexicalEntry} instances which written representation is the
 * specified word.//from ww  w  . j  a va2 s  .c  o  m
 *
 * Optionally lexical entries can be filtered by part-of-speech and a {@link Lexicon}.
 *
 * @param word
 *            the written representation of the lexical entries to be fetched
 * @param pos
 *            the part-of-speech of the lexical entries to be fetched. Set to null in order to
 *            skip part-of-speech filtering and fetch all lexical entries matching other
 *            constraints, regardless of their part-of-speech.
 * @param lexicon
 *            If not null, filters lexical entries by the specified lexicon. Note that the
 *            Lexicon instance has to be obtained beforehand.
 * @return A list of lexical entries matching the specified criteria. If no lexical entry
 *         matches the specified criteria, this method returns an empty list.
 *
 * @see EPartOfSpeech
 * @see LexicalEntry#getLemma()
 */
public List<LexicalEntry> getLexicalEntries(String word, EPartOfSpeech pos, Lexicon lexicon) {
    Criteria criteria = session.createCriteria(LexicalEntry.class);
    if (pos != null) {
        criteria = criteria.add(Restrictions.eq("partOfSpeech", pos));
    }

    if (lexicon != null) {
        criteria = criteria.add(Restrictions.eq("lexicon", lexicon));
    }

    criteria = criteria.createCriteria("lemma").createCriteria("formRepresentations")
            .add(Restrictions.eq("writtenForm", word));

    @SuppressWarnings("unchecked")
    List<LexicalEntry> result = criteria.list();

    if (result == null) {
        result = new ArrayList<LexicalEntry>(0);
    }

    return result;
}

From source file:de.tudarmstadt.ukp.lmf.api.Uby.java

License:Apache License

/**
 * Retrieves a {@link List} of {@link LexicalEntry} instances with lemmas that start with the
 * parameter lemma. E.g. lemma = "leave" -> LexicalEntry with lemma = "leave no stone unturned"
 * is retrieved (among others)//from  w w  w  . ja v a  2 s  .  c o m
 *
 * Optionally lexical entries can be filtered by part-of-speech and a {@link Lexicon}.
 *
 * @param lemma
 *            the lemma the lexical entries has to start with
 * @param pos
 *            the part-of-speech of the lexical entries to be fetched. Set to null in order to
 *            skip part-of-speech filtering and fetch all lexical entries matching other
 *            constraints, regardless of their part-of-speech.
 * @param lexicon
 *            If not null, filters lexical entries by the specified lexicon. Note that the
 *            Lexicon instance has to be obtained beforehand.
 * @return A list of lexical entries matching the specified criteria. If no lexical entry
 *         matches the specified criteria, this method returns an empty list.
 * @see EPartOfSpeech
 * @see LexicalEntry#getLemma()
 */
public List<LexicalEntry> getLexicalEntriesByLemmaPrefix(String lemma, EPartOfSpeech pos, Lexicon lexicon) {
    Criteria criteria = session.createCriteria(LexicalEntry.class);
    if (pos != null) {
        criteria = criteria.add(Restrictions.eq("partOfSpeech", pos));
    }
    if (lexicon != null) {
        criteria = criteria.add(Restrictions.eq("lexicon", lexicon));
    }
    criteria = criteria.createCriteria("lemma").createCriteria("formRepresentations")
            .add(Restrictions.like("writtenForm", lemma + "%"));

    @SuppressWarnings("unchecked")
    List<LexicalEntry> result = criteria.list();
    if (result == null) {
        result = new ArrayList<LexicalEntry>(0);
    }
    return result;
}