Hello
The hibernate version I use is : 3.2.1.
I have a problem when I perform a criteria which contains OR restrictions on a collection. The root object I retrieved has its collection of children objects incomplete. Only the children which correspond to the OR restrictions are in the collection but the other not.
Let's take the following example. The class A has got a Set<B> of the class B.
Code:
// Population step
A a1 = new A();
a1.setName("a1");
for (int i = 0; i < 5; i++) {
B b = new B();
b.setName("b" + i);
// bidirectionnal relation
b.setA(a1);
a1.getBs().add(b);
}
// There is a cascade all to save the attached Bs
session.save(a1);
session.flush();
session.clear();
// Test
Criteria criteria = session.createCriteria(A.class);
Disjunction disjunction = Restrictions.disjunction();
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
criteria.createAlias("bs", "bs", CriteriaSpecification.LEFT_JOIN);
//criteria.createAlias("bs", "bs");
int i = 0;
for(B b : a1.getBs()){
i++;
if (i < 4){
disjunction.add(Restrictions.eq("bs.name", b.getName()));
}
}
criteria.add(disjunction);
List<A> as = criteria.list();
assertEquals(1, as.size());
A a2 = as.get(0);
assertEquals(5, a2.getBs().size());
The last assertion fails. In fact, in the a2.bs (Set<B>), there are only the 3 first b. But even if the 2 last b don't correspond to the OR restrictions, I want to retrieve the whole object A with all its B.
When I do
Code:
criteria.createAlias("bs", "bs");
(default = INNER_JOIN) instead of
Code:
criteria.createAlias("bs", "bs", CriteriaSpecification.LEFT_JOIN)
, it works.
Indeed, when I do that, the collection of b is not populated and a lazy loading is performed when I do a2.getBs(). But this is not the case when I specified LEFT_JOIN. Why the behavior for populating the collection is not the same between INNER_JOIN and LEFT_JOIN. It seems that when we specify LEFT_JOIN it is like "left join fetch" instead of simple "left join".
Anyway, I really need a left join on this request and not an inner join. But the collection is incompletly populated...
Thanks for your help.