I've this structure:
1) class Widget has a lot of MemberTypes (many-to-many)
2) class MemberType has a lot of Members (many-to-many)
I want to get all Page objects that are associated with Member via MemberTypes. I'm trying to do that this way:
Code:
SELECT
w from Widget w, Member m, MemberType mt
WHERE
mt in elements(m.MemberTypes)
AND w in elements(mt.Widgets)
AND (w.WidgetType & 1) <> 0
AND m.ID = 15
ORDER BY w.Name
This can return couple widgets with the same key as Member can be associated with couple MemberTypes and different MemberTypes can be associated with the same Widget. So we can have situation:
MemberA have types
TypeB and
TypeC.
TypeB is associated with
WidgetD and
WidgetE.
TypeC is associated with
WidgetD and
WidgetF. This query then returns
WidgetD,
WidgetE,
WidgetD and
WidgetF. Of course I want
WidgetD only once in list.
For now I just used
DISTINCT like that:
Code:
SELECT DISTINCT
w from Widget w, Member m, MemberType mt (...)
Problem
But it is not the best option as slows down database queries. Is there any approach to this problem so I can start from Member object and list all Widgets that are somehow associated with Widget (in other words "there is a path from Member to Widget")?