views:

139

answers:

1

Hi!

I'm having difficulties with multiple joins in the NHibernate Criteria search. Say I had a pet table, and I wanted to return all pets where the pet category was Dog, and the owner gender was female, ordered by the pet birthday. I've tried a bunch of permutations for how I can get this but haven't been able to figure it out. My latest iteration is as follows:

var recentPets = session.CreateCriteria(typeof(Pet))
            .AddOrder(Order.Desc("PetBirthday"))
            .CreateCriteria("PetType", "pt", JoinType.InnerJoin)
            .CreateCriteria("PetOwnerId", "po", JoinType.InnerJoin)
            .Add(Expression.Eq("pt.PetTypeName", petType))
            .Add(Expression.Eq("po.PersonGender", gender))
            .List<Pet>();

Thanks so much for the help!

+1  A: 

Is there a reason you are not using the Hibernate/Java persistence query language to perform the query?

select p from Pet p
join p.owner o
where o.gender = :gender
and p.type.name = :petType
order by p.birthday
Tomislav Nakic-Alfirevic
Thanks, this works great!
Solomon
Pleased to hear that. :)
Tomislav Nakic-Alfirevic