-->
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: How to model a symmetric self-referencing relation?
PostPosted: Wed Feb 11, 2009 4:38 pm 
Newbie

Joined: Mon Dec 29, 2008 2:20 pm
Posts: 5
Hi,

I am trying to model a friendship relation between User entities. My initial impression was that this should be modeled as a bidirectional m:n relation, much like a parent-child self-reference. But in this case, the roles on both sides of the relation are the same.

In the database schema, I have a join table that basically consists of (user1_id, user2_id). How can I map this relation in the user class so the friends set of a user contains all friends, no matter in which column the user's id is?

In general, are there other ways of accomplishing this in the relational schema? (Although in this case, unfortunately, I have to deal with a legacy schema...)

Thanks,

Michael


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 12, 2009 3:12 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
AFAIK there is no way you can tell hibernate to do this automatically. But what you can do is
    - Either have explicit entries for both sides. ie if 1n 2 are friends then in the many to many link table have two entries (1,2) and (2,1)
    - Or in the actual relationship table you have entry only for 1 side but create a view which will be the union of buddies from both sides and use this view as the relationship table in hibernate.
    - Or in the relationship table you have entry only for 1 side and use the same table as the linking table, but have two Set properties in the entity - one for left and other for right ; and when you call getFriends() method on the User entity, you merge the two lists and return that.


I will opt for the first one but coz in that case your data model agrees exactly with the relational DB. Remember, in a relational DB a relation is symmetrical only if there are tuples (A,B) and (B,A) existing.

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 12, 2009 3:22 am 
Newbie

Joined: Mon Dec 29, 2008 2:20 pm
Posts: 5
Hi,

I was leaning toward solution 1. as well, and your argument regarding correctness is convincing. But will evaluate the view idea (2.) as well (should have thought of that one myself ... ).

Also, thanks for confirming my suspicion that the plain and straightforward solution is actually not possible in Hibernate. Fits the overall picture of Hibernate being zealous about relational correctness (which is a good thing, of course ... ;) )

Regards,

Michael


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.