Give detailed example of ManyToMany with same class at both ends and bidirectional....

Description

I thought I knew Hibernate well but I got stuck with this issue. I am trying to implement a friend relationship between instances of Person class. Naturally, any Person can have any number of friends, which are other instances of Person.

The complication comes from the fact that friend relationship is perfectly symmetric and bi-directional in my application. That is if Person(1) is friend with Person(2), then Person(2) is friend with Person(1) also.

I want to implement this friend relationship in Hibernate using an intermediate class that will hold certain attributes describing further the friendship relationship like date when friendship started, some text note about the friendship,....etc.

Implementing this using Hibernate causes some challenges and even difficulties because I am not sure about how Hibernate handles class IDs internally.

Can you provide an example of Hibernate config file that implements the Many-To-Many on the same class at both ends and with intermediate data class? There should be no order in the relationship (i.e. the relationship is perfectly symmetric).

I suspect that the solution to this issue is to code a composite id class where there is no order between the two ids from Person class. However, I am not sure whether that is sufficient for Hibernate.

Activity

Brett MeyerOctober 30, 2013 at 9:46 AM

This type of question is best started in the user forums: https://forum.hibernate.org/viewforum.php?f=1. We typically use JIRA solely for concise, reproduceable issues with an attached test case or for new feature requests. Thanks!

Rejected

Details

Assignee

Reporter

Components

Priority

Created April 6, 2011 at 6:04 PM
Updated October 30, 2013 at 9:47 AM
Resolved October 30, 2013 at 9:47 AM