-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: mapping a category hierarchy in hibernate
PostPosted: Wed Mar 16, 2005 5:20 pm 
Newbie

Joined: Wed Mar 16, 2005 12:28 pm
Posts: 2
Location: washington, d.c.
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


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 11, 2005 2:36 am 
Newbie

Joined: Thu Apr 07, 2005 3:58 pm
Posts: 2
Hi mcopenha,

Amazingly, this is the same problem I'm having. Other projects i've worked on have encoded this relationship with a 'self-reference' in the entity, instead of the association table, and mapping it is straight forward.

Our mappings look very similar (and probably wrong; help here hibernate experts?). However, I used <many-to-many> since the schema had a two-column link table composed of the 'parent' and 'child' account id's. That's about all I have...seems like I need a schema change but may not be possible.

<!-- Child Accounts -->
<set name="children" lazy="true" cascade="save-update" table="`AccountHierarchy`">
<key column="`ParentAccountID`"/>
<many-to-many class="com.hostingmatrix.account.Account" column="`AccountID`"/>
</set>
<!-- Parent Account -->
<set name="parent" lazy="true" inverse="true" cascade="save-update" table="`AccountHierarchy`">
<key column="`ChildAccountID`"/>
<many-to-many class="com.hostingmatrix.account.Account" column="`AccountID`" unique="true"/>
</set>

- James


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.