In a many-to many relationship between Cat and Dog entities I am given a collection of dogs, and need to find a collection of cats
where each cat is related to all given dogs.
One way of doing it is to join a Cat entity with Dog entity dogs.size() times, but I can't get it to work with Criteria.createAlias() or createCriteria() and I really would like to use Criteria API since my query is pretty dynamic and has many more optional criterias.
In other words, I need something similar to this, that works:
given a Long[] dogid; with 2 IDs in it:
Code:
...
Criteria crit = session.createCriteria(Cat.class)
crit = crit.createAlias("dogs", "dog1")
.createAlias("dogs", "dog2")
.add(Expression.eq("dog1.id", dogid[0]))
.add(Expression.eq("dog2.id", dogid[1]));
crit.list();
...
crashes with an error: table "dog1" is not found.
Hibernate does not allow to have 2 aliases for same collection of docs :(
Any help is greatly appreciated.
Hibernate version: 2.1.2