• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ManyToMany: ternary Relationship

 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have three Entity-Beans:

Person:
row: idUser

Roles:
row: idRole

Group:
row: idGroup

I want to join these via one (and only one) join-table by a ManyToMany-Relation.

The Join-Table in my DATABASE is:

PERSON_ROLE_GROUP
row: ID_PERSON(FK, PK)
row: ID_ROLE (FK, PK)
row: ID_GROUP(FK, PK)

How do I map the ternary ManyToMany-Relationship in Hibernate or better in JPA without creating a new Entity-Bean. Can I do that only by using JPA or have I involving the Hibernate-API?

Is my mapping okay? (I am very unsure about that):



and my non-owing sides are Group and Roles with bidirectionality.

Look at my Group-Entity:



Look at my Role-Entity:



Is my mapping okay or can I do it in a better way?
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay..it seems, that a ternary relationship without an intermediate class is too difficult...

so I create a class 'person_role_group.java' and 'person_role_groupPK.java'.

I do not like this way as I have 2 classes more than needed..but noone really knows how a ternary relationship can be mapped without the two classes..

My Question: Have I override the hascode/equals only in person_role_groupPK.java or in both?
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got some problems by persisting a relationship between the tree entities:




What does it mean??? I only have 3 parameters (properties) in my join-table..so what the hell is the "parameter index 4 is out of range"???
 
goro rogo
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello nimo frey,
I am trying to do exactly the same thing and I really like your approach. Did it work eventually? Too bad that nobody has answered.

Did you finally managed to fix your errors?
what is the use of person_role_groupPK.java?

Can you post the full solution or send it to me by email?

Thank you
 
Omar Al Kababji
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I think the best thing to do is as follow

Person <-- many to many --> Role
Person <-- many to many --> Group
Group <-- many to many --> Role

so you will have three support tables for mapping the many to many relationships

Person_Role
Peron_Group
Group_Role

however since these are supporting tables you will be just mapping many to many relations and there is no need to map those three tables in different beans hibernate will do the work. in your beans you will have the following




hope it helps ;)

(peace)
 
goro rogo
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for responding to this post. Well actually what I want is to have different roles for different users and different groups. For example:

person1 has role1 for the group1
person1 has role2 for the group2 (different role & group)
person2 has role1 for the group1
person2 has role2 for the group1
person2 has role1 for the group2

So according to your suggestion I am associating

Person and Role
Peron and Group
Group and Role

But I need to associate persons with <roles for specific groups>.

Am I getting something wrong? Is the above case scenario example supported by your suggested approach?
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you need an intermediate class, which represent your ternary relationship.

For example:

class A
class B
clas A_B

class A_B is your intermediate class.

You can associate your composite keys via IDClass, or EmbeededID


Look at the hibernate doku and its example about ternary relationship.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic