tags:

views:

9

answers:

0

I just discovered a strange problem when dealing with HQL queries on nested objects.

My class looks like this:

class MyClass {
  MyClass predecessor
}

So using predecessor, I can build a Hierarchy. Now let's say I have a "thin" tree like this:

obj1.predecessor = null
obj2.predecessor = obj1
obj3.predecessor = obj2
obj4.predecessor = obj3

Now when I execute the following query with parameter predecessor:obj1 to get all objects "below" obj1, I only get obj3 and obj4 but not obj2:

select obj from MyClass as obj
where obj.predecessor = :predecessor
or obj.predecessor.predecessor = :predecessor
or obj.predecessor.predecessor.predecessor = :predecessor

I guess this is because for obj2, HQL can not get obj2.predecessor.predecessor.predecessor because obj2.predecessor.predecessor is already null. However I wonder why this matters because this is just one of multiple or-clauses. I would expect that such a line just resolves to "false" but due to another line resolving to "true", obj2 would be included in the results.

Is there any rule saying if I try to get a property on a null object, the object which is matched in this expression is directly suppressed!? Explanation and/or workaround would be great!