Joins with Hibernate Criteria

I found myself needing to find all objects associated to another yesterday using criteria and I wasn't quite sure how to get it done. I've worked with criteria a little (usually I just HQL and call it good) but have never done something quite like this. After digging around a bit i found the solution so I thought I'd share since it wasn't the easiest thing to track down. Let's say you have Person and Role objects where a Person might have many Roles and a Role will, in turn, "have" many Persons. In my case (more or less) I needed to find all Person records that had Role X. The criteria I ended up with looked like this:

Criteria criteria = session.createCriteria(Person.class); Criteria roles = criteria.createCriteria("roles"); roles.add(Restrictions.eq("id", roleID));

I hope this still works. :) I had to adapt it a little from the code I actually used as some of that is irrelevant to what we're talking about here. But this is what finally got me what I needed. Pretty simple. This may not be the most efficient. I haven't examined the generated SQL. If it's not please leave a comment with any suggestions.