Hibernate B-Directed Lots, Lots of Mapping Tips!
I figured if anyone can help me. I'm trying to figure out that for google (or any other ideas !!), basically I have bi-directional many-many mapping between a custom entity and a club entity (via a join table called userClubs). Now I want to include a column in userClubs that represents the role so that when I call user.getClubs () I can also determine what level of access they have. Is there a sane way to do this using hibernate or do I need to rethink the structure of the database? Thanks for any help (or just for that !!!)
user.hbm.xml is a bit like
<set name="clubs" table="userClubs" cascade="save-update">
<key column="user_ID"/>
<many-to-many column="activity_ID"
class="com.ActivityGB.client.domain.Activity"/>
</set>
part of activity.hbm.xml
<set name="members" inverse="true" table="userClubs" cascade="save-update">
<key column="activity_ID"/>
<many-to-many column="user_ID"
class="com.ActivityGB.client.domain.User"/>
</set>
The current userClubs table contains fields id | user_ID | activity_ID
I would like to include id | user_ID | activity_ID | Role
and be able to access the role from both sides ...
a source to share
The easiest way is to create an entity from a join table.
you could use two foreign keys for the club and custom objects as an id (using <composite-id>), but I would suggest adding a technical id column and just set a unique foreign key constraint (otherwise cascade when saving a Club or user won't work.more details here: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html#mapping-declaration-compositeid ).
If you name your new UserClub, you will need one-to-many from User to UserClub and Club to UserClub, and many-to-one from UserClub to club and UserClub to user.
You will have some refactoring for the Java code, but you won't have to change the db (other than adding the id and role columns).
a source to share