My basic scenario is a bi-directional many-to-many relationship that is being mapped with JPA. Simple enough. However, I need to add a 'type' to the mapping, and I'm struggling with the best implementation. Here's the outline:
Network
Set<Member> defaultMembers; //members that meet the network definition
Set<Member> suppressedMembers; //members that meet the network definition, but are hidden.
Set<Member> addedMembers; //memders that don't meet the network definition, but have been added in anyway.
Member
Set<Network> attachedNetworks;
If I didn't need this to be bi-directional (e.g., I only needed to get to members from networks and didn't need to be able to travel the other way), the most obvious solution to me is one link table for each set of members (network_member, suppressed_member, added_member
), but that falls apart going the other way. I suppose I could use a single link table and turn it into an entity with a discriminator column, but every time I've seen someone use a link table as an entity the code seems to turn into a disaster.
I've found many similar questions, but the questions have either been a little too specific, or the answers haven't quite covered the solution I'm seeking. Any suggestions on the best way to handle this situation?
The obvious, portable solutions are the following ones:
Attachment
entity, having a member, a network, and a type of attachment (default, suppressed or added). Have a OneToMany bidirectional association between Network and Attachment, and another OneToMany bidirectional associatio between Member and Attachment.If they don't fit, please explain why.