问题
I have an entities Group and Person with relationships:
Group:
Group.leader -> Person (To One)
Group.looser -> Person (To One)
Group.others ->> Person (To Many)
In leader
, looser
and others
set I could have different Person
entities. Same Person could be leader
in one group, looser
in second and appears in others
set in third group.
in Person
entity I have To-Many relationship groups
which should connect
Person:
Person.groups ->> Group (should be enough but warnings)
Because I can make only one inverse relationship I always will have a warning "something should have inverse"
How to deal with relationships like this?
Or:
I have entities Cube
, Plan
and Line
. Cube
has relationships x
, y
, z
, Plane x
and y
, Line just x
. And I need to share some values between them, even sometimes mixed:
Cube:
Cube.x --> Value
Cube.y --> Value
Cube.z --> Value
Plane:
Cube.x --> Value
Cube.y --> Value
Line:
Cube.x --> Value
Value:
Value.counted -->> Line.x or Line.y, Plane.x, Cube.x, y, z, SomeAnotherEntity.neededValue
回答1:
Apple recommend that every relationship should have an inverse. In your case, that would mean the Person
entity would have three relationships:
Person.groupsLed ->> Group (to many) // "groups where this Person is leader"
Person.groupsLost ->> Group (to many) // "groups where this person is the looser"
Person.otherGroups ->> Group (to many) // "other groups with this person as a member"
which does seem rather complicated. One alternative would be to collapse the three relationships into one (for each of Person
and Group
) with an intermediate entity (Ranking
?):
Group.rankings ->> Ranking (to many) // "the ranking of people for this group"
Person.rankings ->> Ranking (to many) // "the ranking of this person in different groups"
In each case the inverse would be to-one:
Ranking.person -> (Person) (to one) // "the person for this ranking"
Ranking.group -> (Group) (to one) // "the group for this ranking"
You can then add an attribute to the Ranking
entity to indicate the leaders/loosers/other. That could be a simple string attribute rank
which takes the values "leader", "looser" or "other", or an equivalent integer enum. To manage the relationship between a Group
and a Person
, you add or remove Ranking
objects. One downside to all this is that finding the leader or looser involves filtering the rankings, but it does give you a degree of flexibility.
来源:https://stackoverflow.com/questions/46686694/how-to-build-inverse-relationship-to-entity-which-has-more-than-two-relationship