hi,
i have a legacy application that stores a category hierarchy using two tables:
CATEGORIES:
- CATEGORY_ID (PK)
- CATNAME
- TYPE_ID
CATEGORY_HIERARCHY:
- PARENT_CATEGORY_ID
- CHILD_CATEGORY_ID
it's ugly; multiple parentage is allowed and there's no PK on the category_hierarchy table. the main reason it was designed in this way was to support oracle's "connect by prior" mechanism for hierarchical data.
what i'd really like is to map both tables to a single category object -- one that encapsulates both basic info (type, name, id) and hierarchy info. furthermore, it would be nice to have getChildren() and getParents() methods that return Category objects.
i'm not sure this is possible given these tables, but i present some ideas below using what i know.
1. i created a CategoryHierarchy transfer object that just has the 2 fields of the category_hierarchy table.
2. the Category.java class, which is the transfer object for the categories table, has the basic fields of the categories table and 2 sets for parents and children:
Code:
private Set parentCategories = new HashSet();
private Set childCategories = new HashSet();
2. the Categories.hbm.xml file maps the child and parent sets to the category_hierarchy table. for the children, the foreign key is the parent_category_id; for the parents, the child_category_id is the foreign key.
Code:
<set name="childCategories"
inverse="true"
lazy="true">
<key foreign-key="PARENT_CATEGORY_ID">
<key column="CATEGORY_ID"/>
<one-to-many class="CategoryHierarchy"/>
</set>
<set name="parentCategories"
inverse="true"
lazy="true">
<key foreign-key="CHILD_CATEGORY_ID">
<key column="CATEGORY_ID"/>
<one-to-many class="CategoryHierarchy"/>
</set>
am i on the right track? or should i just consider using simple transfer objects for each table and resolve the association using other methods?
thanks,
Mike