Hi,
according to my experience, the Criteria class is generally only to be used for very simple searches such as first-level searches on the object fields, not for sub-object fields. Please make sure that you use HQL for more complex searches or you might find "unable to resolve property" errors.
Criteria are really easy to use:
getSession().createCriteria(MyClass.class).add(Restriction).add(Restriction)..
Restrictions are all subclasses of the Restrictions interface. The most common are Restrictions.eq(propertyName, propertyValue), Restrictions.gt(propertyName, propertyValue) and so on.
For example: this retrieves all the studens with last name equal "Doe":
ArrayList <Student> students = (ArrayList<Student>) getSession().createCriteria(Student.class).add(Restrictions.eq("lastName", "Doe").list();
The following gets students whose last names begin with Doe and whose age is more than 24 (years):
ArrayList <Student> students = (ArrayList<Student>) getSession().createCriteria(Student.class).add(Restrictions.ilike("lastName", "Doe%").add(Restrictions.gt("age", 24).list();
Of course, the Student class must have a lastName and age fields (and possibily the appropriate getters and setters).
|