See the test case for details, but basically, I was trying to create a HQL query that would return a parent object if either its sub-objects matched on a name, or it had no links to its sub-objects. I mistakenly used the identification variable in an is empty instead of the actual collection because I misunderstood that the identification variable is the single object in the collection, not an alias for the collection itself. Whilst this is not the bug, what I believe is the bug, is that hibernate went on to create this sql query:
{code sql } select car0_.id as id1_0_ from car car0_ left outer join car_parts_join_table parts1_ on car0_.id=parts1_.car_id left outer join part part2_ on parts1_.part_id=part2_.id left outer join part_subpart_join_table subparts3_ on part2_.id=subparts3_.part_id left outer join subpart subpart4_ on subparts3_.subpart_id=subpart4_.id where not (exists (select part2_.id from part part2_)) or subpart4_.name='axle' {code}
Instead of just erroring or at least throwing a warning saying that I was using invalid syntax. The problem with this query is that if you look closely at the first where fragment, it will always return true for exists (..), so it will always return false when used in the not(..), therefore will never return any Cars with no parts. This obviously does this logically not make any sense at all if you've not twigged you're using the hql wrong.
Maybe this will not be agreed with, but I feel like what was happening was very unexpected to the user, and this situation would be greatly improved if hibernate could detect whether someone was using a non collection or collection identification variable in an is [not] empty fragment (or indeed any other collection-related fragments). |
|