I've got two objects, object A, object B and C.
Object A has two different sets of B. Object B has many-to-one relationship to C.
I am writing a HQL query like this:
Code:
select a from A a left join a.setB1 as setB1 left join a.setB2 as setB2
where setB1.c.attribute=:someValue and setB2.c.attribute=:someValue
The problem is that the SQL being generated looks like this:
Code:
select a.id as id
from tableA a
left outer join setTableB1 tb1 on a.id=tb1.a_id
left outer join tableB tb on tb1.b_id=tb.id
left outer join setTableB2 tb2 on a.id=tb2.a_id
left outer join tableB tb on tb2.b_id=tb.id,
tableC c1,
tableC c2
where
c1.attribute = "SomeValue" and tb1.c_id=c1.id
c2.attribute = "SomeValue" and tb2.c_id=c2.id
The query returns duplicate results. If I add a distinct clause to the query, it works OK, but performance is horrible (MySQL 4.0.14 sucks CPU), and I only have a few hundred rows in each table.
If I only do one join on either setTableB1 or setTableB2, the query returns quickly without any duplicate results.
Any ideas?