-->
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.  [ 3 posts ] 
Author Message
 Post subject: many-to-many link table
PostPosted: Thu Dec 07, 2006 1:54 am 
Newbie

Joined: Thu Dec 07, 2006 1:28 am
Posts: 4
Hi,

I need some help regarding a special requirement I have for a many-to-many link table.

I have two tables: CATEGORY and TECHNOLOGY which have many-to-many relationship. The mapping files specify the reference (link) table as 'technology_category'

The section of the mapping file (technology.hbm.xml) is shown below:

Code:
      <set name="categories" table="technology_category" cascade="save-update" lazy="true" >
         <key column="technology_id" not-null="true" />
         <many-to-many class="Category" column="category_id"/>
      </set>


The section of the mapping file (category.hbm.xml) is shown below:

Code:
      <set name="technologies" table="technology_category" inverse="true" cascade="save-update" lazy="true" >
         <key column="category_id" not-null="true" />
         <many-to-many class="Technology" column="technology_id"/>
      </set>


Example values in CATEGORY table are similar to FRAMEWORK, TOOL, IDE etc.

Example values in TECHNOLOGY table are HIBERNATE, ECLIPSE etc.

I have no issues in presisting / retrieving the data from any of these tables.

ISSUE:
when Hibernate writes to the link table (CATEGORY_TECHNOLOGY), I want to write other details such as user_id etc, which is available in the web layer.

Here is what I want in the link table.

    category_id, technology_id, user_id


How do I go about doing it?

The mapping only allows only IDs from the participating tables.

Thanks


Top
 Profile  
 
 Post subject: Solution
PostPosted: Fri Dec 08, 2006 11:33 am 
Newbie

Joined: Mon Nov 27, 2006 1:17 pm
Posts: 13
Using many-to-many mapping directly is not recommended to represent a many-to-many relationship between tables. Even though you are not doing the wrong thing but customization like adding more columns to the join table or updation requires something more detailed and specific.
So instead of mapping two tables directly to one another with many-to-many mapping you should use a combination of two one-to-many relationships with the join table.
eg if you have A and B as end tables for whom you want many-to-many relation and you have a third join table A_B.
Your mapping should be
one-to-many :::: A to A_B
many-to-one :::: A_B to B

check these out....
http://forum.hibernate.org/viewtopic.php?p=2331597#2331597
and
http://www.hibernate.org/hib_docs/reference/en/html/best-practices.html
With this you can add extra columns to the join table and map them.This will allow you solve complex situations.

newmax----dont forget to rate me......


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 08, 2006 4:58 pm 
Newbie

Joined: Thu Dec 07, 2006 1:28 am
Posts: 4
Yep, understood -
Quote:
thanks.
Quote:


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.