Hibernate - multiple many-to-many associations between two classes

Viewed 713

I have two entities, call them A and B. Those entities may be in one of three different many-to-many relationships with one another.

How would I model that in Hibernate? The idea is that class A would have fields:

Set<B> relationX;
Set<B> relationY;
Set<B> relationZ;

And similarly class B would have fields:

Set<A> relationX;
Set<A> relationY;
Set<A> relationZ;

The two classes both have all three fields, because I want the association to be bidirectional.

Having the "standard" Hibernate many-to-many annotation like this one...

@JoinTable(name = "A_relX_B",
        joinColumns = {@JoinColumn(name = "A_ID")},
        inverseJoinColumns = {@JoinColumn(name = "B_ID")}
)

... would not work, as there is no way to differentiate between the three separate relations. How would I achieve that? Or do I need to manually decompose the m-to-n relationship?

2 Answers
Related