views:

255

answers:

1

Assume the following exemplary DB-Design:

USERS
-----------------
ID
NAME


ROLES
-----------------
ID
NAME


USERS_ROLES
-----------------
FK_USER_ID
FK_ROLE_ID
LOGIN
PASSWD

I map this relation with eclipse-link using xml. The users:

<many-to-many name="roles" mapped-by="users">
  <join-table name="USERS_ROLES">
    <join-column name="USER_ID" />
    <inverse-join-column name="ROLE_ID" />
  </join-table>
</many-to-many>

For the sake of completeness, the corresponding mapping of the roles:

<many-to-many name="users" mapped-by="roles">
  <join-table name="USERS_ROLES">
    <join-column name="FK_ROLE_ID" />
    <inverse-join-column name="FK_USER_ID" />
  </join-table>
</many-to-many>

Now I could also create a mapping for the link-table, but what I have to do so that I can access the properties of the linked table (mapped to the java object/class)? I need it if the users will login, choose their role of interest and enter the accordant password. With the current setup, of course, I can get all the roles the user is assigned to and vice versa, but I cannot compare against the login data, or any other properties corresponding to the relation of user and role. In short: I have no clue how to access the properties of the linked table or how I have to map this in the mapping-xml.

Thx a lot in advance!

+1  A: 

It isn't an M-N though. It's 2 1-N's with an intermediate class (containing the login and password).

DataNucleus
Thats the general way when the login information are bound to the users, but I need different login information for every user-role relation. So if the users want to login as role1 then they need to enter different login informations as well as they want to login as role2.
VuuRWerK
There are many ways you can do that with classes; I'm simply stating that it is not an M-N relation - there is no such Java container that allows attributes on a relation. Hence you split the attributes into an intermediate class, or have it as a related object off of Role.
DataNucleus
Hm, sry but I can't agree with that. I guess I need a way to create a model/entity which map/represent the link-table with the additional attributes and bind it to the user and role entity. Thus I should have a way to get the information attached to the "link-entity" via the user/role combination. But I dunno how I should realize that binding with such a "link-entity".
VuuRWerK
I already told you to create an intermediate class to represent the attributes. As per http://www.datanucleus.org/products/accessplatform/guides/jdo/many_many_attributed/index.htmlIf you don't want to then fine. It's a solution
DataNucleus
Oh ok ... sry my fault. Thats what I'm search for. Thx a lot! I've also decided to use Annotations instead of the xml mappings. For the posterity I've also found an example to realize it with Eclipse-Link: http://en.wikibooks.org/wiki/Java_Persistence/ManyToMany#Mapping_a_Join_Table_with_Additional_ColumnsThx to DataNucleus!
VuuRWerK