如何与具有两个以上关系的实体建立逆关系? [英] How to build inverse relationship to Entity which has more than two relationship to it?
问题描述
我有一个具有关系的实体Group和Person:
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)
在 leader
, looser
和 others
集中,我可以有不同的 Person
实体.同一个人可能在一组中是 leader
,在第二组中是 loser
,并在第三组中的 others
中出现.
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.
我有一对多关系 groups
应该连接
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"
如何处理这样的关系?
或:我有实体 Cube
, Plan
和 Line
. Cube
具有以下关系: x
, y
, z
,飞机 x
和y
,仅按 x
行.而且我需要在它们之间共享一些价值,甚至有时是混杂的:
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
推荐答案
Apple建议每个关系都应具有逆关系.在您的情况下,这意味着 Person
实体将具有三个关系:
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"
这看起来确实很复杂.一种替代方法是使用中间实体( Person
和 Group
中的每一个):
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"
然后,您可以将属性添加到 Ranking
实体,以指示领导者/放松者/其他人.那可能是一个简单的字符串属性 rank
,它使用值"leader","loser"或"other"或等效的整数枚举.要管理 Group
和 Person
之间的关系,可以添加或删除 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.
这篇关于如何与具有两个以上关系的实体建立逆关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!