Hey man, which Version of hibernate are you using? I used 3.1.2 which has an Criteria.createAlias(String,String,int) Method. The int value sets the join mode.
I'm not sure whether this method is included in your version of hibernate but the following code does the job... I actually don't really know why the query above doesn't work... the sql looks quite alright.
Code:
Criteria crit = session.createCriteria(Adult.class)
.createAlias("children","child",CriteriaSpecification.LEFT_JOIN);
crit.add(Restrictions.or(Restrictions.eq("child.sex","m")
.ignoreCase(),Property.forName("children").isEmpty()));
yours simon
bremmington wrote:
Thanks Simon. This does get very close to the right result. However I'm either still doing it wrong, or this is failing to retrieve adults who have no children.
The SQL that this is generating is something like:
Code:
select
this_.ID,
this_.NAME,
child1_.ID,
child1_.NAME,
child1_.SEX,
child1_.PARENTID
from
ADULTS this_
inner join
CHILDREN child1_ on this_.ID=child1_.PARENTID
where
not exists (select 1 from CHILDREN where this_.ID=PARENTID)
or child1_.SEX='M'
The problem is that the inner join on CHILDREN is causing any adults with no children to be missed. This is a pity, because the where clause looks perfect.
If you (or anyone else) can suggest a solution to this, I'd be very grateful. It seems so close :)
Regards
Brian