Red Hat Training

A Red Hat training course is available for Red Hat JBoss Web Server

15.4. Associations

By navigating associations using createCriteria() you can specify constraints upon related entities:
List cats = sess.createCriteria(Cat.class)
    .add("name", "F%") )
        .add("name", "F%") )
The second createCriteria() returns a new instance of Criteria that refers to the elements of the kittens collection.
There is also an alternate form that is useful in certain circumstances:
List cats = sess.createCriteria(Cat.class)
    .createAlias("kittens", "kt")
    .createAlias("mate", "mt")
    .add( Restrictions.eqProperty("", "") )
(createAlias() does not create a new instance of Criteria.)
The kittens collections held by the Cat instances returned by the previous two queries are not pre-filtered by the criteria. If you want to retrieve just the kittens that match the criteria, you must use a ResultTransformer.
List cats = sess.createCriteria(Cat.class)
    .createCriteria("kittens", "kt")
        .add( Restrictions.eq("name", "F%") )
Iterator iter = cats.iterator();
while ( iter.hasNext() ) {
    Map map = (Map);
    Cat cat = (Cat) map.get(Criteria.ROOT_ALIAS);
    Cat kitten = (Cat) map.get("kt");