-->
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.  [ 6 posts ] 
Author Message
 Post subject: Same class many-to-many (lovers' problems)
PostPosted: Thu Nov 03, 2005 9:41 am 
Newbie

Joined: Thu Nov 03, 2005 9:28 am
Posts: 2
Location: Santiago
I have a Person class, mapped to a PERSON table.
I need to express a many-to-many simmetric relationship between persons: the lovers relationship.

The lovers relationship is a simmetrical one, so if one person break the relationship, both break (like in real life). And just like in real life, a person can have many lovers (0..*).

In tradicional data base modeling, we can think of a LOVERS table, with two columns (lover1 and lover2), each one keeping a primary keys to the PERSON table. In traditional db modeling, we can recover lovers making an SQL 'select * from LOVERS where lover1 = person.primaryKey or lover2 = person.primaryKey'

How can model this relationship in Hibernate? How can recover the many lovers for a person?

Restriction: I want just one entry in the LOVERS table for each pair of person having a lovers relationship.

Thanks

_________________
Andres Grino


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 03, 2005 9:54 am 
Senior
Senior

Joined: Thu Aug 04, 2005 4:54 am
Posts: 153
Location: Birmingham, UK
I'd have an object called Relationship which has two properties, lover1 and lover2. Or if you're really adventurous it could have a set of People for relationships involving more than one person.

Person ---- Relationship ----- Person


With each person being able to belong to 0 or more relationships. Then if a relationship becomes unbalanced it can be deleted.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 03, 2005 11:16 am 
Newbie

Joined: Thu Nov 03, 2005 9:28 am
Posts: 2
Location: Santiago
jamie_dainton wrote:
I'd have an object called Relationship which has two properties, lover1 and lover2. Or if you're really adventurous it could have a set of People for relationships involving more than one person.

Person ---- Relationship ----- Person


With each person being able to belong to 0 or more relationships. Then if a relationship becomes unbalanced it can be deleted.


I know, but how I model this in Hibernate.

I can use a Set in Person, but the link to LOVERS is to a specific column (lover1).

What I want is to link to lover1 OR lover2. How I write this in a .xml.hbm?

Thank anyway

_________________
Andres Grino


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 05, 2005 3:31 am 
Regular
Regular

Joined: Thu Oct 27, 2005 8:06 am
Posts: 55
Location: München, Germany
I guess it can't be done by using the documented syntax of Hibernate's mapping files.

The problem seems to stem from the inherent assymetry of your relationship table. Having entries named lover1 or lover2 means that, formally, there is a difference, even if semantically you don't intend it to be.

Therefore, it appears primarily a data modeling problem to me, not a Hibernate problem. Hibernate just follows your data model.

A symmetrical data model looks like this: The relationship entity has two attributes: relationship_id and participant_id. For each relationship, you would now have as many records as there are participants. That means 2 for a classical relationship, but as you are liberal enough to allow anyone as many relationships as she or he wants, why not also allow relationships with more than one participant...


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 05, 2005 3:40 am 
Regular
Regular

Joined: Thu Oct 27, 2005 8:06 am
Posts: 55
Location: München, Germany
Afterthought: The reason it's difficult or maybe impossible with the lover1, lover2 approach is inserting. Even if there was a way to express in the mapping file to map the same lover field in Person to both lover1 and lover2, how could poor Hibernate know, when you insert a new couple relationship, which of the persons to map to column 1, which to column 2? As I said, Hibernate can't know that semantically it makes no difference.

For read access, there might be a cute way to solve this via a database view. You can get a symmetrical view at your relationship table by defining a view as a union of (lover1, lover2) with (lover2, lover1). So, although physically you only have one record, the view allows you to select all lovers of one person in the standard way that Hibernate would understand. However, this view isn't updateable.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 05, 2005 5:13 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
you can create a *read-only* collection that is based on a native sql query.

inserts must be done elsewhere.

_________________
Max
Don't forget to rate


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