将实体与 Neo4j 中正确的关系相关联? [英] Relating an entity to a relationship proper in Neo4j?

查看:20
本文介绍了将实体与 Neo4j 中正确的关系相关联?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Neo4j 为项目、员工和项目角色之间的关系建模.每个项目都有一个称为项目经理"的角色和一个称为主管"的角色.我试图在数据模型中实现的是能够说对于项目 A,主管是员工 X".就我而言,重要的是项目"、人员"和角色"都是实体(而不是属性).这在 Neo4j 中可能吗?简单来说,在 Neo4j 中关联实体是否可行?在 MySQL 中,这将用一个带有唯一 id 列和三个外键列的联结表表示,一个分别用于项目、人员和角色,这将允许我将这些实体之间的关系标识为实体本身.想法?

I'm attempting to use Neo4j to model the relationship between projects, staff, and project roles. Each project has a role called "project manager" and a role called "director". What I'm trying to accomplish in the data model is the ability to say "for project A, the director is staff X." For my purposes, it's important that "project", "staff", and "role" are all entities (as opposed to properties). Is this possible in Neo4j? In simpler terms, are associative entities possible in Neo4j? In MySQL, this would be represented with a junction table with a unique id column and three foreign key columns, one for project, staff, and role respectively, which would allow me to identify the relationship between those entities as an entity itself. Thoughts?

推荐答案

@wassgren 的回答很可靠,值得考虑.

@wassgren's answer is a solid one, worth considering.

我会提供一个额外的选择.也就是说,您可以具体化"这种关系.当您建立关系并将其变成节点时,具体化是一种.您正在采用抽象关联(员工和项目之间的关系)并将其转换为具体实体(角色)所有其他答案选项基本上涉及两个节点 Project员工,他们之间的关系有所不同.这些方法不会具体化角色,而是将其存储为关系的属性或标签.

I'll offer one additional option. That is, you can "Reify" that relationship. Reificiation is sort of when you take a relationship and turn it into a node. You're taking an abstract association (relationship between staff and project) and your'e turning it into a concrete entity (a Role) All of the other answer options involve basically two nodes Project and Staff, with variations on relationships between them. These approaches do not reify role, but store it as a property, or a label, of a relationship.

(director:Staff {name: "Joe"})-[:plays]->(r:Role {label:"Director"})-[:member_of]->(p:Project { name: "Project X"});

所以...人们不会直接为项目做出贡献,角色会.人们扮演角色.这很直观.

So...people don't contribute to projects directly, roles do. And people play roles. Which makes an intuitive sense.

这种方法的优点是您可以将角色"视为一等公民,并断言有关它的关系和属性.如果您不将角色"拆分为一个单独的节点,您将无法将关系挂在该节点之外.此外,如果您向伪装成角色的关系添加额外的属性,您可能最终会混淆属性何时应用于角色,以及何时应用于员工与项目之间的关联.

The advantages of this approach is that you get to treat the "Role" as a first-class citizen, and assert relationships and properties about it. If you don't split the "Role" out into a separate node, you won't be able to hang relationships off of the node. Further, if you add extra properties to a relationship that is masquerading as a role, you might end up with confusions about when a property applies to the role, and when it applies to the association between a staff member and a project.

想知道谁在参与项目?就是这样:

Want to know who is on a project? That's just:

  MATCH (p:Project {label: "Project X"})<-[:member_of]-(r:Role)<-[:plays]-(s:Staff)
   RETURN s;

所以我认为我的建议从长远来看更灵活,但对你来说也可能有点矫枉过正.

So I think what I'm suggesting is more flexible for the long term, but it might also be overkill for you.

考虑一个假设的未来需求:我们希望将角色与技术级别或工作类别相关联.IE.项目经理应该始终是 VP 或更高(愚蠢的例子).如果你的角色是一段关系,你就不能那样做.如果您的角色是合适的节点,则可以.

Consider a hypothetical future requirement: we want to associate roles with a technical level or job category. I.e. the project manager should always be a VP or higher (silly example). If your role is a relationship, you can't do that. If your role is a proper node, you can.

这篇关于将实体与 Neo4j 中正确的关系相关联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆