I have a situation in which I have a many-to-many relation split up in two one-to-many relations using a join table. Normally, each id of the join table referes to some unique entity. For example; Blog - BlogUser - User
After setting up the relations in Blog and User, you can ask a Blog for its collection of users, and inverse, you can ask a User for its Blogs.
Now, my problem is a bit more complex. I have don't have 'unique' entities, in fact, the link table maps id's of the same table.
So I have; User - UserEquality - User
The goal that UserEquality denodes the id's of users which are 'equal'.
Imagine I have three users:
Code:
User_Id UserName
1 Gerald
2 Geraald
3 Gerold
All three acutally represent the same user.
The linktable would then have these entries:
Code:
UserId_A UserId_B
1 2
1 3
2 3
I could map two "one to many" relations in "User" each on a different id. So I would have :
User.hbm.xml
Code:
<bag name="userEqualityA">
<key column="UserId_A" />
<one-to-many entity-name="UserEquality" />
</bag>
<bag name="userEqualityB" cascade="all-delete-orphan">
<key column="UserId_B"/>
<one-to-many entity-name="UserEquality" />
</bag>
This is all good, I have two relations on the same entity.
Now, with this setup I want to lookup the users that are equal to "Geraald". The problem is that I need a union of both relations. Retrieving "userEqualityA" is only half of the solution ; I only get "Gerold" back.
The thing is, with blog and users you have a clearly defined relation between them. Asking a blog for its users always returns the complete story for that relation; you get what you asked for; all the users registered for that blog. Also, if you ask a user for its blogs; you get what you asked for; all the blogs on which this user has registered.
With this user equality all sides of the relation are 'equal' , there is no pre-defined order. So the correct answer on "give me the equal users" would be a "joined" collection of both the userEqualityA and userEqualityB relation.
So, I'm looking for a way in hibernate to return a 'joined' collection of both userEqualityA and userEqualityB. I don't want to map these relations to my entity objects, since the user would be a ble to only use one of them (and thats not correct; you always need both relations to have a correct view of equal users)[/code]