We have a requirement for a class which can have many children of the same type.
We map this composite-pattern type class as having a bag of children mapped as a many-to-many relationship:
Code:
<hibernate-mapping>
<class
name="com.foo.domain.Entity"
table="entity_table"
dynamic-update="false"
dynamic-insert="false"
>
...
<bag
name="children"
table="parent_child_hierarchy"
lazy="false"
inverse="false"
cascade="none"
>
<key
column="parent"
/>
<many-to-many
class="com.foo.domain.Entity"
column="child"
outer-join="true"
/>
</bag>
...
</hibernate-mapping>
However, unlike the composite pattern described in the wiki page, we do not have only single leafs, rather we can have many leaves of children. Thus, in our child class we do not have a M:1 relationship to the parent since it can potentially have many parents.
We handle the circular dependency issues in our business logic.
Question:If we do a query on this type, for example:
Code:
from Entity as e where e.user= :userId
What is the most efficient way to return only "Enitity" classes which are not orphaned?
At one point we considered including a M:M relationship inside the entity that would contain a collection of parents. This would essentially be the same as the children mapping, except that it would key on the the child, rathen than the parent.
In that way we could have appended
Code:
where e.parents.size > 0
Any guidance would be appreciated.
Regards,
Roll