-->
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: Mapping set to multiple foreign key columns
PostPosted: Mon Mar 06, 2006 3:48 am 
Newbie

Joined: Mon Mar 06, 2006 3:22 am
Posts: 3
Hello,
I have a class that contains a Set that links to another table through two columns and I am trying to figure out if it is possible to configure a set in my hbm that will work.
In my domain I have a Person who can have a relation to another Person. Because I want to be able to specify the relationship, I made a Relation class which contains two Persons, Person1 and Person2. When retrieving a set from tha database for any person, a query should be executed to collect all Relations that are linked to either Person1 or Person2. IN SQL I would use a query like:
select * from person p left join relation r on r.id_person1 = p.id OR r.id_person2 = p.id
I am not sure if I explained the situation correctly but this is my best try :-). Can anyone tell if and how this can be done?
thx.
scobie


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 06, 2006 6:10 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
I don't know if it works semantically for your situation, but it seems that there is automatically an implied ordering to the relationship, and that should be recognized. For instance, even in your hypothetical SQL, the relation is only "interesting" in the sense that it gets you to the Person on the other side. But in order to find that Person, you need to know whether the "source" person is person1 or person2 in the relationship:

Code:
select *
from person p
   left outer join relation r1 on (r1.id_person1 = p.id)
   left outer join relation r2 on (r2.id_person2 = p.id)
   left outer join person p2 on (p2.id = r1.id_person2)
   left outer join person p1 on (p1.id = r2.id_person1)


Given that, the following seems like a workable mapping:

Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" >
   <class name="Person" >
      ...

      <set name="Up_Relations" >
         <key column="id_Person1" />
         <one-to-many class="Relation" />
      </set>
      
      <set name="Down_Relations" >
         <key column="id_Person2" />
         <one-to-many class="Relation" />
      </set>
      
   </class>
   
   <class name="Relation" >
      ...
      <property name="RelationType" />
      
      <many-to-one name="Person1" class="Person" />
      <many-to-one name="Person2" class="Person" />
   </class>
</hibernate-mapping>


I'm not sure there is any way to implement a completely unordered relationship. Sometimes the order isn't needed, which is to say that while it must needs exist, it has no meaning; it may be completely arbitrary (like a generated object ID). It may be possible to "spoof" a set of relations on top of the "Up" and "Down" sets. Then simply choose a set to which new relations will be added--it doesn't matter which.

Of course, all of this is just speculation. You might research about data structures for holding vertices and their connecting edges in graph theory research--that would appear to be an exact analogue to your situation.

Good luck!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 09, 2006 11:22 am 
Newbie

Joined: Mon Mar 06, 2006 3:22 am
Posts: 3
Hi Marcal,
Thanks for your reply - it's very helpful. If I understand things, it is not possible to map a collection in which the order has no meaning (which is the case).
I did succeed in mapping two collections, one up and one down as you suggested, and using the union method to combine them into the collection I really want, This works, the only drawback is that it needs two statements to fill the collections.
I will look into 'data structures for holding vertices and their connecting edges in graph theory research', as you suggested but at the moment that does not mean much to me :-0


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.