findByCriteria in hibernate

In my generic Hibernate DAO which consists of a method findByCriteria

protected List<T> findByCriteria(Criterion... criterion) {
        Criteria crit = getSession().createCriteria(getPersistentClass());
        for (Criterion c : criterion) {
            crit.add(c);
        }
        return crit.list();
    }


Now myDao extends generic dao.Now how can i use findByCriterion to get objects satisfying certain conditions.Can any one give some examples?

Answer

I do not know what dao.Now is but, you can use this method like this:

List<YourObject> result = findByCriteria(Restrictions.like("name", "Fritz%"));

Assuming that YourObject has a property name that should begin with Fritz

See documentation

Leave a Reply

Your email address will not be published. Required fields are marked *