-->
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: How to map M:N recursive relationship with join table
PostPosted: Sun Mar 21, 2010 7:07 am 
Newbie

Joined: Sun Mar 21, 2010 4:35 am
Posts: 1
I have already seen a mapping example of recursive relationship that uses a foreign key on the same table. As for my case, the recursive relationship is using a cross-reference table to resolve many-to-may relationship. The following should illustrate the DB structure.

Image

Basically, a part has a list of substitute parts. Conversely, a part can also be a substitute of several primary parts.

From a class perspective, I would assume that they can just use the same class named Part. Only that if it's a primary part, it will have a list of substitute parts and if it's a substitute part, it will have a linkage to its primary parts.

My question is:

How can I map the substitute parts in the Part class so that it uses the same class as part? Is this possible in the first place? Take note that the primary part would have to query the PART_SUBSTITUTE_XREF table first and get the substitute part numbers before querying back to the PART table and use those part numbers to get the details of the substitute parts.

The following SQL statement should illustrate what I want to happen.
Code:
select substitute.*
from PART part
left outer join PART_SUBSTITUTE_XREF xref on part.part_no = xref.part_no
left outer join PART substitute on substitute.part_no = xref.sub_part_no
where part.part_no = 798


Top
 Profile  
 
 Post subject: Re: How to map M:N recursive relationship with join table
PostPosted: Mon Mar 22, 2010 6:06 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
How can I map the substitute parts in the Part class so that it uses the same class as part?


It should work with a mapping like following, mapping defining a bidirectional many-to-many relation.
In this way hibernate automatically maps the relations through a join table (aka cross-table).

Code:
@Entity
public class Part {
  @Id ...
  long part_no;

   @ManyToMany()
   protected Set<Part> issubstitute = new HashSet<Part>();

   @ManyToMany(mappedBy="issubstitute")
   protected Set<Part> substitutes = new HashSet<Part>();
}



If you need that the cross-table is named exactly 'PART_SUBSTITUTE_XREF',
then you can additionally use the @SecondaryTable(name="PART_SUBSTITUTE_XREF") annotation.


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.